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 ...
随机推荐
- 怎样单独遍历NodeList的键、值和键值对
1. 单独遍历键: NodeList.prototype.keys(); 2. 单独遍历值: NodeList.prototype.values(); 3. 遍历键值对: NodeList.proto ...
- 怎样修改输入框 placehoder 提示文本的颜色?
1. 在这个问题上, 不同浏览器的设置方法有所差异, 可以写成下面这种形式. ::-webkit-input-placeholder { /* WebKit browsers */ color: #9 ...
- Lua访问网页
示例 例子,实现https方式,登录网站,访问某个网页,修改其中参数的功能.其中xx应用时候需要修改. require("curl") local ipList = { " ...
- luogu题解P4198楼房重建--线段树神操作
题目链接 https://www.luogu.org/problemnew/show/P4198 分析 一句话题意,一条数轴上有若干楼房,坐标为\(xi\)的楼房有高度\(hi\),那么它的斜率为\( ...
- haproxy实现ssl套接字加密
概述 如果你的应用使用SSL证书,则需要决定如何在负载均衡器上使用它们. 单服务器的简单配置通常是考虑客户端SSL连接如何被接收请求的服务器解码.由于负载均衡器处在客户端和更多服务器之间,SSL连接解 ...
- 反序列化报错回显、反弹shell
• 使用java.net.URLClassLoader类,远程加载自定义类(放在自己服务器上的jar包),可以自定义方法执行. • 在自定义类中,抛出异常,使其成功随着Jboss报错返回命令执行结果. ...
- collections:内建模块,提供额外的集合类
介绍 collections里面包含了很多除了内置类型之外的数据类型,我们使用它们有时可以很方便的完成一系列操作 ChainMap:搜索多个字典 from collections import Cha ...
- pandas中Series对象下的str所拥有的方法(df["xx"].str)
在使用pandas的时候,经常要对DataFrame的某一列进行操作,一般都会使用df["xx"].str下的方法,但是都有哪些方法呢?我们下面来罗列并演示一下.既然是df[&qu ...
- kettle Spoon.bat闪退解决办法
1.Java环境配置问题 java_home:D:\Program Files\Java\jdk1.7.0_25(安装jdk路径) classpath:.;%java_home%\lib\dt.jar ...
- 8.6.zookeeper应用案例_分布式共享锁的简单实现
1.分布式共享锁的简单实现 在分布式系统中如何对进程进行调度,假设在第一台机器上挂载了一个资源,然后这三个物理分布的进程都要竞争这个资源,但我们又不希望他们同时 进行访问,这时候我们就需要一个协调器, ...