gpt4 book ai didi

python - 不断获取元素未附加到页面文档

转载 作者:太空宇宙 更新时间:2023-11-03 15:34:32 25 4
gpt4 key购买 nike

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

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

我尝试在每个步骤后添加 time.sleep(2) 但当我运行此函数时它仍然给我一个错误 ->> 在这行代码中继续获取元素未附加到页面文档错误

self.row.find_element_by_xpath(ListViewLocatars.VERIFYTEXT % Text)

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

 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

最佳答案

这是如何触发 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

如何避免它:

@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)
# only now self.row is defined and even if page refreshed
# next time you call self.row, element will be re-defined

关于python - 不断获取元素未附加到页面文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42630717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com