python selenium错误陈旧元素:元素未附加到页面文档

时间:2018-07-09 15:23:23

标签: python selenium

我正试图从一个名为Correios的网站获取所有数据,在此网站中,我需要处理一些下拉菜单,但遇到一些问题,例如:它无法从第一个下拉框信息中获取所有值。 / p>

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)

我该怎么办?

2 个答案:

答案 0 :(得分:1)

根据回溯,从opt2.click中抛出例外。我猜想,只要您从estados下拉列表中选择一个选项,就会相应地更新municipio下拉列表中的可用选项。这意味着在选择第二个estado之后从municipio_select.options获得的选项不再有效,这就是为什么会出现该异常的原因。

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

答案 1 :(得分:0)

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

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