【问题标题】:python selenium error stale element: element is not attached to the page documentpython selenium错误陈旧元素:元素未附加到页面文档
【发布时间】:2018-07-09 15:23:22
【问题描述】:

我正在尝试从一个名为 Correios 的网站获取所有数据,在这个网站上,我需要处理一些下拉列表,我遇到了一些问题,例如:它没有从第一个 Dropbox 信息中获取所有值。

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.select import Select


chrome_path = r"C:\\Users\\Gustavo\\Desktop\\geckodriver\\chromedriver.exe"

driver = webdriver.Chrome(chrome_path)
lista_x = []
driver.get("http://www2.correios.com.br/sistemas/agencias/")
driver.maximize_window()

estado_select = Select(driver.find_element_by_id('estadoAgencia'))
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'municipioAgencia')))
municipio_select = Select(driver.find_element_by_id('municipioAgencia'))
for opt in estado_select.options:
    print(opt.get_attribute('innerHTML'))
    opt.click()
    for opt2 in municipio_select.options:
        print(opt.get_attribute('innerHTML'))
        opt2.click()

driver.close()

有时我的代码运行正常,但有时它会给我这个错误:

ACRE
ACRE
ALAGOAS
ALAGOAS
Traceback (most recent call last):
  File "C:\Users\Gustavo\Desktop\insper\trabalho\Correios3.py", line 23, in <module>
    opt2.click()
  File "C:\Users\Gustavo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\Gustavo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\Gustavo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Gustavo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, 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.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.17134 x86_64)

我该怎么办?

【问题讨论】:

  • 还是出现同样的错误,我要放新代码

标签: python selenium


【解决方案1】:

根据回溯,异常是从opt2.click 抛出的。我想每当您从 estados 下拉列表中选择一个选项时,municipio 下拉列表中的可用选项都会相应更新。这意味着您在选择 2nd estado 后从 municipio_select.options 获得的选项不再有效,这就是您得到该异常的原因。

为了解决这个问题,请移动该行: municipio_select = Select(driver.find_element_by_id('municipioAgencia'))opt.click() 之后(在外循环内部,就在内部for 循环之前)。这将导致内部循环遍历 更新的 municipio 选项列表,应该可以解决您的问题。

【讨论】:

    【解决方案2】:

    让我们假设estado_select = Select(driver.find_element_by_id('estadoAgencia')) 返回 2 个链接。 然后您遍历网址estado_select。在第一次迭代期间(opt 包含第一个链接),当您执行opt.click() 时,您的浏览器会加载一个新页面,因此第二个链接现在已过时。所以在第二次迭代期间opt.click 会失败并生成错误StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

    您必须在每个click 之后执行driver.navigate().back(),否则您必须再次解析新页面并再次构建estado_selectmunicipio_select

    【讨论】:

    • 但两个下拉菜单都在同一个页面中,URL 永远不会改变
    • 即使 URL 永远不会改变,元素的可见性也会改变。如果第二个链接现在变得不可见,您可能会遇到此问题
    猜你喜欢
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 2020-06-29
    • 2023-03-24
    相关资源
    最近更新 更多