gpt4 book ai didi

python - 使用 selenium 刷新导致 StaleElementReferenceException 错误的页面

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

我有一个使用 selenium 的 python 脚本。脚本的步骤是:

  1. 登录
  2. 转到启动文件收集器进程的页面
  3. 每 1 分钟刷新一次状态页面以检查文件收集器是否已完成 - 这在页面上的表格中显示

我遇到的问题是,当使用 browser.refresh() 刷新页面时,出现以下错误

Traceback (most recent call last):
File "D:\ScheduledTasks\Scripts\ScriptArchive\COL_INSPIRE\INSPIRE_METADATA_v1.1.py", line 491, in <module>
print head.text
File "C:\Python27\ArcGIS10.2\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webelement.py", line 61, in text
return self._execute(Command.GET_ELEMENT_TEXT)['value']
File "C:\Python27\ArcGIS10.2\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webelement.py", line 385, in _execute
return self._parent.execute(command, params)
File "C:\Python27\ArcGIS10.2\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 173, in execute
self.error_handler.check_response(response)
File "C:\Python27\ArcGIS10.2\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 166, in check_response
raise exception_class(message, screen, stacktrace)
StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up

这曾经有效,但现在出现上述错误,“轮询”页面以检查一段文本是否已更改的最佳方法是什么。

我的代码是

header = browser.find_elements(By.TAG_NAME,"tr")
#go through each header to get teh one we want
headerIndex = 0
for head in header:

#print headerIndex
print head.text
if "Next harvest" in head.text:
#Get the table data for the header that we want
tdata = header[headerIndex].find_elements(By.TAG_NAME,"td")
for t in tdata:
print t.text
if "Scheduled" in t.text:
#wait 60 seconds
time.sleep(60)
browser.refresh()
elif "Not yet scheduled" in t.text:
refreshComplete = True
break
if refreshComplete == True:
break
headerIndex = headerIndex + 1

最佳答案

每次刷新页面时都必须找到标题。

我还会切换到使用 find_element(s)_by_xpath 来检查文本:

while True:
header = browser.find_element_by_xpath('//tr[contains(., "Next harvest")]')

not_scheduled = header.find_elements_by_xpath('//td[contains(., "Not yet scheduled")]')
if not_scheduled:
break

time.sleep(60)
browser.refresh()

希望您已经了解解决方案背后的想法(但请检查我是否遵循了正确的逻辑)。

关于python - 使用 selenium 刷新导致 StaleElementReferenceException 错误的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29238649/

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