有时候循环点击一列链接,只能点到第一个,第二个就失败了

原因是第二个已经是新页面,当然找不到之前页面的元素。就算是后退回来的,页面也是不一样的

页面长的一样不一定是同一张页面,就像两个人长的一样不一定是同一个人,他们的身份证号不同,页面,甚至页面上的元素都是有自己的身份证号(id)的

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('http://www.baidu.com') print(driver.find_element_by_id('kw')) # kw before refresh driver.refresh() # refresh print(driver.find_element_by_id('kw')) # kw after refresh driver.quit()
"""
<selenium.webdriver.remote.webelement.WebElement (session="eaad4403c91da0736e9f3218a4d2ea7d", element="0.9658163881659727-1")>
<selenium.webdriver.remote.webelement.WebElement (session="eaad4403c91da0736e9f3218a4d2ea7d", element="0.7123759980029825-1")>
"""

我们发现,仅仅是刷新了一下页面,两次的element id是不同的,这就说明这是两个不同的元素,如果用以下的方式来定位,自然会因为找不到而报错

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('http://www.baidu.com') kw = driver.find_element_by_id('kw') # 先定位并获得kw元素
kw.click() driver.refresh() # refresh kw.click() # 刷新后,仍用原来的kw元素操作,这时会报错 driver.quit()
"""

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.1.7601 SP1 x86_64)

"""

原因很明显,你用别人的身份证id去找现在的人,哪怕这两个人长的很像,他也会告诉你,对不起,你找错人啦

refresh,无论你是主动刷新还是页面自动刷新

back,已经跳转到了其他页面,然后你用driver.back()跳回来,这也是一张新的页面了

跳转到了新的页面,但这张新页面上有一些元素跟之前页面是长的一样的,这也是一张新的页面了。比如:一排分页按钮,你点击下一页跳转到了第二页,想要还用原来的元素操作到下一页,那也是不可能的了

除此之外可能还有其他的原因,总之看到类型差不多,但是对页面有了操作,就应该想想这种可能性

遇到这种情况怎么办:

很简单:

刷新页面之后重新获取元素就行,不要提前获取一组元素,然后去循环操作每一个元素,这种情况还是获取元素的个数,然后在循环中获取相应位置的元素,在用的时候才去获取,这样你就获取到最新的id了

遇到页面有变化的情况,不要去循环元素,去循环个数或定位方式,在循环中获取元素

selenium之坑(StaleElementReferenceException: Message: Element not found in the cache...)的更多相关文章

  1. selenium之 坑(StaleElementReferenceException: Message: Element not found in the cache...)

    今天给大家分享一个selenium中经常会有人遇到的坑: selenium.common.exceptions.StaleElementReferenceException: Message: Ele ...

  2. selenium之坑:点击后页面刷新重新获取刷新前的页面(StaleElementReferenceException:Message:Element not found in the cache...)

    循环点击一列链接,但只能点到第一个,第二个失败,这是为什么,因为第二个已经是新页面,当然找不到之前页面的元素,虽然元素没变,甚至是后退回来,页面都没有变,为什么是新页面,页面长的一样不代表是同一张页面 ...

  3. selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

    抓取网页代码后,由于是在同一个li标签下,所以使用一次性抓取,所有的a标签,然后循环做不同的操作,但是抛出找不到元素异常. def office_page(_chrome: Chrome): sn = ...

  4. 爬虫-Selenium -抱错ElementNotVisibleException: Message: element not visible

    1.当使用Selenium IDE 完成了脚本的录制和回放通过后,想要将脚本转换为其他语言如java.Python等,首次使用时打开Options->Format发现没有可以转换的语言,如下: ...

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

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

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

  7. selenium+python自动化88-批量操作循环点击报错:Element not found in the cache - perhaps the page has changed since it was looked up

    前言 selenium定位一组元素,批量操作循环点击的时候会报错:Element not found in the cache - perhaps the page has changed since ...

  8. Webdriver如何解决页面元素过期:org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up

    当运行Webdriver时抛出如下异常:org.openqa.selenium.StaleElementReferenceException: Element not found in the cac ...

  9. python+selenium:点击页面元素时报错:WebDriverException: Message: Element is not clickable at point (1372.5, 9.5). Other element would receive the click: <li style="display: list-item;" id="tuanbox"></li>

      遇到一个非常郁闷的问题,终于解决了, 问题是这样的,NN网站的价格计划,每一个价格计划需要三连击才能全部点开,第一个房型的价格计划是可以正确三连击打开的,可是第二个房弄就不行了,报错说不是可点击的 ...

随机推荐

  1. TCP/IP详解 卷一(第十二章 广播和多播)

    广播和多播仅应用于UDP. 广播指的是一个主机向网上的所有其他主机发送帧,而 多播仅发送给属于多播组的多个主机. 为了弄清广播和多播,需要了解主机对由信道传送过来帧的过滤过程 1.首先,网卡查看由信道 ...

  2. group by having和connect by

    --使用group by 子句对数据进行分组:对group by 子句形成的组运行聚集函数计算每一组的值:最后用having 子句去掉不符合条件的组.--having 子句中的每一个元素也必须出现在s ...

  3. gopath基础概念

    GOROOT golang安装路径. GOPATH 官方解释,请google.go工作环境中常常用到的一个很重要的环境变量(这种设计类似java).具体用途:go命令常常需要用到的,如go run,g ...

  4. Mqtt协议IOS端移植2

    MqttFramework.h #import <Foundation/Foundation.h> #import "MQTTClient.h" #import &qu ...

  5. 笔记本中G-Sensor(加速计) M-Sensor 陀螺仪等传感器的区别与作用

    1.G-sensor重力传感器 作用 G-sensor中文是加速度传感器的意思(英文全称是Accelerometer-sensor),它能够感知到加速力的变化,加速力就是当物体在加速过程中作用在物体上 ...

  6. atitit.bsh BeanShell 的动态脚本使用java

    atitit.bsh BeanShell 的动态脚本使用java 1.1. BeanShell是一个小巧免费的JAVA源码解释器 ,支持对象式的脚本语言特性,亦可嵌入到JAVA源代码中. 亦可嵌入到J ...

  7. springBoot+springCloud学习笔记

    尊重原创:https://www.jianshu.com/p/492dfefa2735 SpringBoot 配置优先级 在命令行中传入的参数 如:java -jar storeMs.jar --se ...

  8. java 原生定时执行程序(ScheduledExecutorService)

    package ThreadPoolTest; import java.util.Date; import java.util.concurrent.*; public class Main { pu ...

  9. MapReduce Input Split 输入分/切片

    MapReduce Input Split(输入分/切片)详解 public static long getMaxSplitSize(JobContext context) { return cont ...

  10. sam配置命令概要

    ./install_packages --enhanced_reporting katello-configure --deployment=sam --user-pass=redhat chkcon ...