selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
抓取网页代码后,由于是在同一个li标签下,所以使用一次性抓取,所有的a标签,然后循环做不同的操作,但是抛出找不到元素异常。
def office_page(_chrome: Chrome):
sn = '官网首页'
_xpath = '//li[@class="scNav_Item"]/a'
sc_nav_list = _chrome.driver.find_elements_by_xpath('//li[@class="scNav_Item"]/a')
print(len(sc_nav_list))
for item in sc_nav_list:
print(item)
png = _chrome.click_element_screen_shoot(item)
save_picture(sn, png)
time.sleep(1)
png = _chrome.pull_down_screen_shoot()
save_picture(sn, png)
屏幕日志:
(robot) E:\Project\Robot\domain>E:/virtualenvs/robot/Scripts/python.exe e:/Project/Robot/domain/run.py
DevTools listening on ws://127.0.0.1:55976/devtools/browser/e36c0dea-4bd8-4f49-b910-812e5f5542fa
5
<selenium.webdriver.remote.webelement.WebElement (session="3bc66d5ecc5f15a0fc6f125f6ec323e7", element="93079042-927d-49af-8424-44979cacf9cc")>
<selenium.webdriver.remote.webelement.WebElement (session="3bc66d5ecc5f15a0fc6f125f6ec323e7", element="e330b1bf-b80e-4c7c-a6f9-9f33ffc6c721")>
Traceback (most recent call last):
File "e:/Project/Robot/domain/run.py", line 111, in <module>
run(url)
File "e:/Project/Robot/domain/run.py", line 78, in run
office_page(chrome)
File "e:/Project/Robot/domain/run.py", line 60, in office_page
png = _chrome.click_element_screen_shoot(item)
File "e:\Project\Robot\domain\web.py", line 63, in click_element_screen_shoot
self.click_element(element)
File "e:\Project\Robot\domain\web.py", line 53, in click_element
ac(self.driver).move_to_element(element).click().perform()
File "E:\virtualenvs\robot\lib\site-packages\selenium\webdriver\common\action_chains.py", line 80, in perform
self.w3c_actions.perform()
File "E:\virtualenvs\robot\lib\site-packages\selenium\webdriver\common\actions\action_builder.py", line 76, in perform
self.driver.execute(Command.W3C_ACTIONS, enc)
File "E:\virtualenvs\robot\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "E:\virtualenvs\robot\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=77.0.3865.90)
最后原因是因为刷新页面导致之前拿到的元素对象失效,无法锁定,从而导致改报错。
坑:
- 页面点击之后刷新了
- 之前抓取的元素对象失效
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document的更多相关文章
- robotframework执行用例时,报错selenium.common.exceptions.WebDriverException: Message: unknown error: cannot get automation extension from unknown error: page could not be found: chrome-extension://aapnijgdinl
在用robotframework编写移动端测试用例(用chrome浏览器模拟手机浏览器),执行用例时, 报错selenium.common.exceptions.WebDriverException: ...
- selenium.common.exceptions.ElementNotVisibleException: Message: element not visible处理方法:selenium针对下拉菜单事件的处理
使用Selenium爬虫时,可能会遇到一些下拉菜单,动态加载,如果直接使用find_element_by_函数会报错,显示selenium.common.exceptions.ElementNotVi ...
- python + web自动化,点击不生效,提示“selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (117, 674)”
前言: 在做web自动化时,遇到一个缩放了浏览器比例的操作,从100%缩小到80%,再进行点击的时候,弹出报错信息,无法点击 selenium.common.exceptions.ElementCli ...
- Python3+selenium 报错处理:“selenium.common.exceptions.NoAlertPresentException: Message: No alert is active”
一.说明 在使用python3+selenium写自动升级程序的时侯,碰到一个弹出对话框需要点击确认的场景.弹出的对话框如下图所示. 对于弹框各种资料都说通过switch_to.alert属性获取对话 ...
- selenium.common.exceptions.TimeoutException: Message: Screenshot: available via screen
在使用selenium+phantomjs的时候在Windows平台下能够正常工作,在Linux下却不能,并得到错误信息: selenium.common.exceptions.TimeoutExce ...
- selenium使用遇到的问题(selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH.)
1.安装pip3 install selenium 2.使用browser=webdriver.Chrome()时报错 :selenium.common.exceptions.WebDriverExc ...
- python+selenium,打开浏览器时报selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH
有一年多没写web自动化了,今天搭建环境的时候报了一个常见错误,但是处理过程有点闹心,报错就是常见的找不到驱动<selenium.common.exceptions.WebDriverExcep ...
- 【Selenium】【BugList7】执行driver.find_element_by_id("kw").send_keys("Selenium"),报错:selenium.common.exceptions.InvalidArgumentException: Message: Expected [object Undefined] undefined to be a string
[版本] selenium:3.11.0 firefox:59.0.3 (64 位) python:3.6.5 [代码] #coding=utf-8 from selenium import webd ...
- 关于Selenium.common.exceptions.WebDriverException: Message: Invalid locator strategy: css selector 的问题
在执行脚本时报Selenium.common.exceptions.WebDriverException: Message: Invalid locator strategy: css selecto ...
随机推荐
- Spring 的 Bean 管理(XML 方式)
Spring 的 Bean 管理(XML 方式) 1. 三种实例化 Bean 的方式 使用类构造器实例化(默认无参数) 使用静态工厂方法实例化(简单工厂模式) 使用实例工厂方法实例化(工厂方法模式) ...
- spring boot 学习笔记(三)之 配置
一:概述 在Spring boot 中根据业务需求,我们往往会在不同地方配置我们所需的key-value 配置项,配置文件存在不同的地方的场景如下: (1) 默认存在 application.prop ...
- SQLite数据库_c/s架构的心得
1.使用是Navicat Premium软件, Microsoft Windows版本. 2.选择SQLite并新建数据库: 3.将建好的SQLite数据库,放到新建的项目的debug文件下中, 并在 ...
- whistle学习(一)之安装、使用、软件功能了解
前言 whistle是基于Node实现的跨平台抓包调试代理工具,有以下基本功能: 查看HTTP.HTTPS请求响应内容 查看WebSocket.Socket收发的帧数据 设置请求hosts.上游htt ...
- 如何解决windows 80端口被占用的情况
window下如果在安装某个软件或者启动tomcat时 会遇到80端口被占用的情况,通过命令 netstat -ano 查看端口被占用情况 找到占用80端口的进程ID,然后我们在任务管理器当中将这个进 ...
- postman传数组参数,二维数组,多维数组
一维数组: 传递: 接收: 二维数组: 传递: 接收: 依此类推,
- SQL基础:语句执行顺序
SQL入门 select * from table; SQL实战题目 有下面一个表 t ,存储了每个商品类别的成交明细,我们需要通过下面这张表获取订单量大于10对应的类别,并从中取出订单量前3的商品类 ...
- nginx 配置反向代理和负载均衡
Nginx的配置文件: nginx安装目录/conf/nginx.conf 重新加载配置文件 ./nginx -s reload 配置虚拟主机 一个server就是一台虚拟主机 server { li ...
- HTML5中新增加的结构元素、网页元素和全局属性
HTML5新增的结构元素(新增的都是块元素,独占一行) 1) header 定义了文档的头部区域 <header> <h1>网站标题<h1> </header ...
- JavaScript中变量声明效率问题
1 var theString1 = "字符串1"; var theString2 = "字符串1"; var theString3 = "字符串1& ...