我有一个使用selenium的python脚本。脚本的步骤是:
我遇到的问题是当使用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
答案 0 :(得分:2)
每次刷新页面时都必须找到标题。
我还会转而使用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()
希望您已经了解解决方案背后的想法(但请检查我是否正确遵循了逻辑)。