Python 如何解决PyTest中的过时元素异常
在下面的测试中,我点击上面的每张卡片,看看标题文本是否匹配。我遇到了一个StaleElementReferenceException,我的假设是,Python 如何解决PyTest中的过时元素异常,python,selenium,webdriver,pytest,Python,Selenium,Webdriver,Pytest,在下面的测试中,我点击上面的每张卡片,看看标题文本是否匹配。我遇到了一个StaleElementReferenceException,我的假设是,Internications变量在单击卡片后变得过时,即使我使用self.driver.back()返回到我单击的页面 这类问题是否有解决办法?或者我需要为每张卡创建单独的测试吗?如果是这样的话,PyTest是否有一个内置模块,允许我们轻松创建单独的测试 def test_internship_links(self): num_title_mi
Internications
变量在单击卡片后变得过时,即使我使用self.driver.back()
返回到我单击的页面
这类问题是否有解决办法?或者我需要为每张卡创建单独的测试吗?如果是这样的话,PyTest是否有一个内置模块,允许我们轻松创建单独的测试
def test_internship_links(self):
num_title_mismatch = 0
title_mismatches = {}
internships_xpath = (
"//div[@id='related_projects']//div[@class='project-description']/h3"
)
internships = self.driver.find_elements(By.XPATH, internships_xpath)
for internship in internships:
outer_title = internship.text
internship.click()
inner_title = self.driver.find_element(
By.XPATH, "//div[@class='primary']//h1"
).text
if inner_title != outer_title:
num_title_mismatch += 1
title_mismatches[outer_title] = inner_title
self.driver.back()
assert (
num_title_mismatch == 0
), f"\n{num_title_mismatch} title mismatch(es) found:\n{title_mismatches}"
错误回溯:
________________________________________________________________________ TestSearchInterships.test_internship_links ________________________________________________________________________
self = <test_search_internships.TestSearchInterships object at 0x1081bfe90>
def test_internship_links(self):
num_title_mismatch = 0
title_mismatches = {}
internships_xpath = (
"//div[@id='related_projects']//div[@class='project-description']/h3"
)
internships = self.driver.find_elements(By.XPATH, internships_xpath)
for internship in internships:
> outer_title = internship.text
tests/test_search_internships.py:41:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../../.local/share/virtualenvs/converge-testing-PXQ7_xev/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py:81: in text
return self._execute(Command.GET_ELEMENT_TEXT)['value']
../../../.local/share/virtualenvs/converge-testing-PXQ7_xev/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py:693: in _execute
return self._parent.execute(command, params)
../../../.local/share/virtualenvs/converge-testing-PXQ7_xev/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py:338: in execute
self.error_handler.check_response(response)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu________________________________________________________________________
自我=
def测试实习链接(自我):
num\u title\u不匹配=0
标题_不匹配={}
实习(实习)(
“//div[@id='related_projects']//div[@class='project-description']/h3”
)
interniships=self.driver.find_元素(By.XPATH,interniships_XPATH)
对于实习中的实习:
>外部标题=实习.text
测试/测试搜索实习。py:41:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../...local/share/virtualenvs/converge-testing-PXQ7_xev/lib/python3.7/site packages/selenium/webdriver/remote/webelement.py:81:文本格式
返回self.\u执行(Command.GET\u元素\u文本)['value']
../../...local/share/virtualenvs/converge-testing-PXQ7_xev/lib/python3.7/site packages/selenium/webdriver/remote/webelement.py:693:in
返回self.\u parent.execute(命令,参数)
../../...local/share/virtualenvs/converge-testing-PXQ7_xev/lib/python3.7/site packages/selenium/webdriver/remote/webdriver.py:338:in execute
self.error\u handler.check\u响应(响应)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
如果页面上的DOM(文档对象模型)操作暂时导致元素无法访问,则可能会发生此问题
我可以为您的问题找到两种可能的解决方法
1) 不要使用self.driver.back()
返回页面,您可以再次导航到该链接。e、 g.:driver.get('your_url')
2) 在最终引发异常之前,可以尝试在循环中多次访问该元素。您可以查看Darrel Grainger的解决方案
如果我有帮助,请给我竖起大拇指=)计算元素的数量,使用范围而不是循环元素本身,这对我来说很有帮助。下面是工作代码
def test_internship_links(self):
num_title_mismatch, title_mismatches = 0, {}
internships = self.driver.find_elements(
By.XPATH, "//div[@id='related_projects']//a"
)
# xpath is indexed from 1, not 0
for int_num in range(1, len(internships) + 1):
internship_xpath = f"//div[@id='related_projects']//a[{int_num}]//div[@class='project-description']//h3"
internship = self.driver.find_element(By.XPATH, internship_xpath)
outer_title = internship.text
internship.click()
inner_title_xpath = "//div[@class='primary']//h1"
inner_title = self.driver.find_element(By.XPATH, inner_title_xpath).text
if inner_title != outer_title:
num_title_mismatch += 1
title_mismatches[outer_title] = inner_title
self.driver.back()
assert_str = (
f"\n{num_title_mismatch} title mismatch(es) found:\n{title_mismatches}"
)
assert num_title_mismatch == 0, assert_string
请将完整的错误回溯添加到您的问题。上面添加了感谢,但建议的解决方法都没有解决StaleElementReferenceException错误