抓取网页代码后,由于是在同一个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的更多相关文章

  1. 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: ...

  2. selenium.common.exceptions.ElementNotVisibleException: Message: element not visible处理方法:selenium针对下拉菜单事件的处理

    使用Selenium爬虫时,可能会遇到一些下拉菜单,动态加载,如果直接使用find_element_by_函数会报错,显示selenium.common.exceptions.ElementNotVi ...

  3. 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 ...

  4. Python3+selenium 报错处理:“selenium.common.exceptions.NoAlertPresentException: Message: No alert is active”

    一.说明 在使用python3+selenium写自动升级程序的时侯,碰到一个弹出对话框需要点击确认的场景.弹出的对话框如下图所示. 对于弹框各种资料都说通过switch_to.alert属性获取对话 ...

  5. selenium.common.exceptions.TimeoutException: Message: Screenshot: available via screen

    在使用selenium+phantomjs的时候在Windows平台下能够正常工作,在Linux下却不能,并得到错误信息: selenium.common.exceptions.TimeoutExce ...

  6. 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 ...

  7. python+selenium,打开浏览器时报selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH

    有一年多没写web自动化了,今天搭建环境的时候报了一个常见错误,但是处理过程有点闹心,报错就是常见的找不到驱动<selenium.common.exceptions.WebDriverExcep ...

  8. 【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 ...

  9. 关于Selenium.common.exceptions.WebDriverException: Message: Invalid locator strategy: css selector 的问题

    在执行脚本时报Selenium.common.exceptions.WebDriverException: Message: Invalid locator strategy: css selecto ...

随机推荐

  1. 怎样通过id属性快速从HTMLCollection对象中获取到目标元素节点

    方法1: 直接使用id或name属性: 比如我想获取 id 为 img1 的图片元素节点, 则可以这样写: document.images.img1 如果没有对应节点, 则返回undefined; 方 ...

  2. java语言中使用三元式的时候应该注意的问题

    今天在项目中改领导要求的代码表现的时候发现了一个很有趣的问题. 但是的代码情况类似如下: 1 2 Integer test1 = null; System.out.println("test ...

  3. [C#.net]C#如何解析json文本文件

    C#解析Json字符串,可以借助Newtonsoft.Json将Json字符串序列化为对象,再从对象中获取值 Newtonsoft.Json.JsonConvert.DeserializeObject ...

  4. c# word excel text转html的方法

    首先是预览图片,这个功能很好实现,无非就是创建一个html页面,嵌套一个<img>,为了限制图片类型,可以定义一个允许预览类型数组作为限制: /// <summary> /// ...

  5. git的详细安装

    git的详细安装 Git 是时候动手尝试下 Git 了,不过得先安装好它.有许多种安装方式,主要分为两种,一种是通过编译源代码来安装:另一种是使用为特定平台预编译好的安装包. 从源代码安装 若是条件允 ...

  6. 洛谷题解P4314CPU监控--线段树

    题目链接 https://www.luogu.org/problemnew/show/P4314 https://www.lydsy.com/JudgeOnline/problem.php?id=30 ...

  7. JavaScript使用纯函数避免bug

    纯函数 一.纯函数 定义:纯函数是指不依赖并且不修改其作用域之外的函数.通过以下几个示例来认识纯函数: var a = 10; //纯函数 function foo(num){ return num ...

  8. Html-自适应

    自适应 使网页能适应不同终端设备的技术.原理是通过检测视口分辨率来判断是什么终端的,PC,手机还是平板. 做自适应的网页时,需要在代码中加入“祖传代码”,即通用代码. 这是在头部head引入的: &l ...

  9. 几种常用的java 实现反转的方法———reverse

    1.最简单的方法 public static String reverse1(String str) return new StringBuffer(str).reverse().toString() ...

  10. vue-element-admin 多层路由问题

    在二级页面上添加<router-view> 关于页面打包后三级路由无法访问问题 需要将 存放router-view 的页面单独存放一个文件夹 router.js 写法