抓取网页代码后,由于是在同一个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. 题解 UVA1316 【Supermarket】

    题目链接: https://www.luogu.org/problemnew/show/UVA1316 思路: 根据题目意思,我们需要用到贪心的思想,越晚过期的商品当然是越晚卖好.同时你假如有多个商品 ...

  2. shiro学习(四、shiro集成spring+springmvc)

    依赖:spring-context,spring-MVC,shiro-core,shiro-spring,shiro-web 实话实说:web.xml,spring,springmvc配置文件好难 大 ...

  3. H.264 详解

    一.MPEG-4说明 1.VOP视频编码技术 VO(Video Object):视频对象,它是场景中的某个物体,最简单的情况下就是矩形框,它是有生命期的,由时间上连续的许多帧构成. VOP(Video ...

  4. git创建库

    WMW@WMWGO MINGW64 /f $ cd e:               # 切换到 E 盘 WMW@WMWGO MINGW64 /e $ mkdir learngit      # 创建 ...

  5. 如何将webstrom本地的代码上传到github上

    首先注册一个github账户,然后下载一个git软件. 文件夹的任意处点击右键,找到gitbash here,打开终端命令窗口.  因为我们本地 Git 仓库和 GitHub 仓库之间的传输是通过 S ...

  6. java ftp retrieveFile 较大文件丢失内容

    今天发现用  如下方法下载一个2.2M的zip文件但是只下载了500K没有下载完全,但是方法  返回的却是true boolean org.apache.commons.net.ftp.FTPClie ...

  7. 1如何给devexpress的gridview控件绘制全选按钮

    1 首先注册gridview的this.edibandedGridView.CustomDrawColumnHeader += EdibandedGridView_CustomDrawColumnHe ...

  8. Mybatis和hibernate的优缺点比较

    介绍: Hibernate :Hibernate 是当前最流行的ORM框架,对数据库结构提供了较为完整的封装. Mybatis:Mybatis同样也是非常流行的ORM框架,主要着力点在于POJO 与S ...

  9. mint-ui下拉加载min和上拉刷新(demo实例)

    <template> <div class="share"> <div class="header"> <div cl ...

  10. deep_learning_Activate_method

    常见的激活函数有sigmoid.tanh和relu三种非线性函数,其数学表达式分别为: sigmoid: y = 1/(1 + e-x) tanh: y = (ex - e-x)/(ex + e-x) ...