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 ...
随机推荐
- Cesium中的样条插值
Cesium中的样条插值 在cesium里,提供了三种样条插值方法,LinearSpline,CatmullRomSpline,HermiteSpline.在具体的实例上,可以使用样条插值法利用已知的 ...
- http://www.moext.com博客搬家到这里啦
1.原博客莫叉特用的是自己的域名http://www.moext.com,由于服务器在国外,访问不太稳定,SEO做得也很不好: 2.喜欢博客园的极简风格,目前来看广告量也在可接受范围: 3.一个偶然的 ...
- ETL 工具和 BI 工具
ETL是数据仓库中的非常重要的一环,是承前启后的必要的一步.ETL负责将分布的.异构数据源中的数据如关系数据.平面数据文件等抽取到临时中间层后进行清洗.转换.集成,最后加载到数据仓库或数据集市中,成为 ...
- MySQL通过 LOAD DATA INFILE 批量导入数据
LOAD DATA INFILE 语句用法 参考手册 本文语句参数使用默认值 PHP: TP框架环境 // 定义文件路径$file_path = 'LOAD_DATA_LOCAL_INFILE.tx ...
- Task扩展方法取消操作
/// <summary> /// 任务扩展,传入取消操作 /// </summary> public static class TaskExtensionDemo { //因 ...
- OJ问题集合
1.找出给定数组之中的3个和为0的数. 2.给出一个字符串,给出需要去除的字符串,返回去除需要去除的字符串后的值. 3.一个数组之中的第二最大值三个数的组合.
- Python中的操作符及优先级
附注: 1. Python中的按位运算符是把数字看作二进制来进行计算的.Python中的按位运算法则如下: 按位与 ( bitwise and of x and y ) & 举例: 5 ...
- sql 实现取表中相同id时间最大的一行 利用distinct on
数据表是这样的 select * from water_level_records m where ( select count(*) from water_level_records n where ...
- HTML符号代码速查表
HTML实体符号被用作实现保留字符(reserved characters)或者表达键盘无法输入的一些常用字符.在大多数浏览器中默认的字符集为ISO-8859-1.HTML实体符号使我们在网页设计中经 ...
- Java io 理解
任何程序都有io部分,io是对程序来说数据流的输入和输出.这里说的流,是指有字节组成的列,不断输入程序,或者从程序中输出,我们形象称为流.Java的io流有两种,一种叫字节流,最原始的:一种叫字符流. ...