Keep getting元素未附加到页面文档

2024-09-28 03:14:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在运行以下代码以验证页面上的文本:

def verifyText(self, Text):
    try:
       self.switchToFrame(*MainPageLocatars.FRAMEONE)
       self.switchToFrame(*MainPageLocatars.SUBLISTFRAME)
    except:
       pass
    self.row.find_element_by_xpath(ListViewLocatars.VERIFYTEXT % Text)

我试着补充时间。睡觉(2) 在每一步之后,当我运行此函数时仍然会给我一个错误->Keep get element not attached to the page document error at this line code

^{pr2}$

我在这里调用函数,我应该在哪里重新定义它?在

 listview = ListView(self.driver, 'First')
 listview.verifyText("comp1")

请注意,行是父行:

self.row = self.driver.find_element_by_xpath(ListViewLocatars.ROWPARENT % INDEX_MAP[index])

我是这样定义函数的:

class ListView(Page):

    def __init__(self, driver, index):

        if index not in INDEX_MAP:
            raise ValueError("Invalid index %s" % index)

        self.driver = driver

        try:
            self.row = self.driver.find_element_by_xpath(ListViewLocatars.ROWPARENT % INDEX_MAP[index])
        # used for VerifyText function only
        except:
            self.row = self.driver.find_element_by_xpath(ListViewLocatars.TEXTPARENT % INDEX_MAP[index])


    def verifyText(self, Text):
        try:
            self.switchToFrame(*MainPageLocatars.FRAMEONE)
            self.switchToFrame(*MainPageLocatars.SUBLISTFRAME)
        except:
            pass
        self.row.find_element_by_xpath(ListViewLocatars.VERIFYTEXT % Text)

以下是完整代码:

# all locaters for this class are defined here only
class ListView(Page):

    def __init__(self, driver, index):

        if index not in INDEX_MAP:
            raise ValueError("Invalid index %s" % index)

        self.driver = driver

        try:
            self.row = self.driver.find_element_by_xpath(ListViewLocatars.ROWPARENT % INDEX_MAP[index])
        # used for VerifyText function only
        except:
            self.row = self.driver.find_element_by_xpath(ListViewLocatars.TEXTPARENT % INDEX_MAP[index])

    @property       
    def row(self):
        return self.driver.find_element_by_xpath(ListViewLocatars.ROWPARENT % INDEX_MAP[index])


    def verifyText(self, Text):
        try:
            self.switchToFrame(*MainPageLocatars.FRAMEONE)
            self.switchToFrame(*MainPageLocatars.SUBLISTFRAME)
        except:
            pass
        self.row.find_element_by_xpath(ListViewLocatars.VERIFYTEXT % Text)

它现在给我这个错误:

Traceback (most recent call last):
  File "autoLoaderTest.py", line 56, in test02_AutoLoaderSubCompany
    listview = ListView(self.driver, 'First')
  File "C:\Users\cverma\Desktop\SOAPProject\mainPage.py", line 44, in __init__
    self.row = self.driver.find_element_by_xpath(ListViewLocatars.TEXTPARENT % INDEX_MAP[index])
AttributeError: can't set attribute

Tags: textselfmapindexbydefdriverelement
1条回答
网友
1楼 · 发布于 2024-09-28 03:14:58

这是如何触发StaleElementReferenceException以及如何避免它的简化版本。在

我想您的代码如下所示:

self.row = self.driver.find_element_by_xpath(ListViewLocatars.ROWPARENT % INDEX_MAP[index])
# Now your self.row is accessible. You can use it
self.driver.find_element_by_xpath('//input[@type="text"]').send_keys('some data')
# This action could trigger page refresh
# Now your self.row is not accessible as it was found on previous page
self.row.find_element_by_xpath(ListViewLocatars.VERIFYTEXT % Text)
# Here comes StaleElementReferenceException

如何避免:

^{pr2}$

相关问题 更多 >

    热门问题