猿问

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

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


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)


我应该怎么办?


人到中年有点甜
浏览 352回答 2
2回答
随时随地看视频慕课网APP

相关分类

Python
我要回答