Selenium Python:循环中间-陈旧元素错误

时间:2018-06-26 13:43:45

标签: python loops selenium iterator staleelementreferenceexception

因此,我的代码尝试通过链接列表(所有链接的可见文本均为“年度”),然后在新网页上时,在年份下拉列表中迭代20年左右并下载每年的数据集。因此有两个循环。我的代码陷入了Years循环-也就是说,对于相同的“ Annual”,两年后会收到陈旧的元素错误。

我正在开发chrome,这是我代码的相关部分:

Links = browser.find_elements_by_link_text("Annual")
for link in Links:
    link.click()
    time.sleep(20)
    browser.switch_to_window(browser.window_handles[-1])
    select = Select(browser.find_element_by_xpath('//select[@name="year"]'))
    options = select.options
    for index in range(0, len(options)):
        select.select_by_index(index)
        time.sleep(10)
        excelbutton = browser.find_element_by_xpath('//a[img[@title="Download as Excel"]]').click()

错误如下: selenium.common.exceptions.StaleElementReferenceException:消息:陈旧元素引用:元素未附加到页面文档

将感谢您的帮助!我尝试通过答案,但是由于我正在使用2个循环,并且其中一个循环工作了2年后效果很好,所以我无法弄清楚。

更新:完整的错误消息:

Traceback (most recent call last):
  File "abc", line 56, in <module>
    select.select_by_index(index)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/support/select.py", line 99, in select_by_index
    for opt in self.options:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/support/select.py", line 47, in options
    return self._el.find_elements(By.TAG_NAME, 'option')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 527, in find_elements
    {"using": by, "value": value})['value']
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 493, in _execute
    return self._parent.execute(command, params)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 249, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 193, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=67.0.3396.99)
  (Driver info: chromedriver=2.40.565386 (45a059dc425e08165f9a10324bd1380cc13ca363),platform=Mac OS X 10.11.6 x86_64)

1 个答案:

答案 0 :(得分:2)

Update-1

由于在您的情况下,过期元素位于选择框中,因此您需要像下面那样更改代码

select = Select(browser.find_element_by_xpath('//select[@name="year"]'))
options = select.options
for index in range(0, len(options)):
    select.select_by_index(index)
    time.sleep(10)
    excelbutton = browser.find_element_by_xpath('//a[img[@title="Download as Excel"]]').click()
    select = Select(browser.find_element_by_xpath('//select[@name="year"]'))
    options = select.options

原始答案

单击页面上的链接后,页面可能会重新加载或更改,因此您的元素集合变得陈旧。因此,您需要应用不同的策略来克服相同的问题

Links = browser.find_elements_by_link_text("Annual")
link_count = len(Links)

for link_index in range(0, link_count):
    link = browser.find_elements_by_link_text("Annual")[link_index]

    link.click()
    time.sleep(20)
    browser.switch_to_window(browser.window_handles[-1])
    select = Select(browser.find_element_by_xpath('//select[@name="year"]'))
    options = select.options
    for index in range(0, len(options)):
        select.select_by_index(index)
        time.sleep(10)
        excelbutton = browser.find_element_by_xpath('//a[img[@title="Download as Excel"]]').click()

以上代码假定返回到页面时,链接数保持不变。也不是说最好的代码。但这是解决问题的一种方法。

另一种解决方法是获取每个链接的href,然后导航到它们。当然,只有当您没有基于JavaScript的链接时,此方法才有效