为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

人到中年有点甜 2021-04-04 12:13:23
我正在尝试从一个名为Correios的网站获取所有数据,在此网站中,我需要处理一些下拉菜单,这些菜单出现一些问题,例如:它无法从第一个下拉框信息中获取所有值。from selenium import webdriverfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support.select import Selectchrome_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()有时我的代码运行正常,但有时却出现此错误:ACREACREALAGOASALAGOASTraceback (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)我应该怎么办?
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 351 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号