1.找不到元素

写脚本的过程中时不时就会报这种错,一般路径定位不到直接复制xpath基本就能找到了,也有时候是因为有iframe或是句柄不对

原因:

①没有加等待时间,脚本运行到那步时,页面还没加载完,所以就找不到元素了,加上Thread.sleep(2000)就好了

②页面元素xpath路径中含有动态数字:

例如这种://*[@id="el-collapse-content-8913"]/div/div/div[1]/div/div/div/div/div/input,在这个地址中,每次刷新页面8913都会变,所以直接定位显然是徒劳的

得改成这种://*[contains(@id,'el-collapse-content')]/div/div/div[1]/div/div/div/div/div/input

具体可参考这位大佬的博客:https://www.cnblogs.com/dingxinwen/p/14192929.html

2.元素被遮挡导致找不到

就是在定位下面这个下拉框的元素集合时,一直报错说找不到元素

一顿百度后,说是下拉框下的元素集合和下拉框不在一个地方……于是我定位了一下还真不在一个地方,之前从没注意过还有这种问题


定位下拉框如下:


再定位下拉框元素集合如下:


下面这个下拉框是常见的,元素集合和下拉框在一起

然后就重新定位了元素集合,结果运行还是报错,报错信息如下:

org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <li class="el-select-dropdown__item">...</li> is not clickable at point (262, 379). Other element would receive the click: <div class="el-form-item__content" style="margin-left: 100px;">...</div>
(Session info: chrome=94.0.4606.61)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'DESKTOP-F3AC8H1', ip: '192.168.2.162', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_151'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 94.0.4606.61, chrome: {chromedriverVersion: 94.0.4606.41 (333e85df3c9b6..., userDataDir: C:\Users\caiman\AppData\Loc...}, goog:chromeOptions: {debuggerAddress: localhost:63208}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: 02d316c1613deed0338da698e5931b6e
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
at com.dnt.auto.NewTest.f(NewTest.java:74)

这个问题百度各位大佬说是当前要点击的元素被其它元素覆盖了,所以要把焦点移动到这个元素后才能点击,给了如下两种解决方法:

方法一:
element = driver.find_element_by_css('div[class*="loadingWhiteBox"]')

driver.execute_script("arguments[0].click();", element) 方法二:
element = driver.find_element_by_css('div[class*="loadingWhiteBox"]')

webdriver.ActionChains(driver).move_to_element(element ).click(element ).perform()

我选了第一种方法,用java改了一下:

//      点击“商户”下拉框
driver.findElement(By.xpath("//*[contains(@id,'el-collapse-content')]/div/div/div[1]/div/div/div/div/div/input")).click();
// 点击商户下拉框时,由于webdriver只能操作页面可见的元素,所有需要下垃滚动条定位元素
JavascriptExecutor js1 = (JavascriptExecutor)driver;
// 循环下拉,向下拉95000
js1.executeScript("scrollTo(0,95000)");
WebElement select = driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[1]/ul/li[27]"));
// 需要执行下面这个js后才能找到元素
JavascriptExecutor js2 = (JavascriptExecutor)driver;
js2.executeScript("arguments[0].click();",select);

这样就能定位到了。(先定位元素,再执行js)

PS:关于“ JavascriptExecutor”的介绍可参考这位大佬的博客:https://www.cnblogs.com/lnn123/p/10209641.html

 3.当页面上两个按钮元素地址一样时(2021-11-9)

(这次碰到的问题是页面上有很多折叠按钮,操作过程中需要展开或收起各个版块,但是定位发现每个按钮的xpath路径是一样的)

eg:下图是第一个按钮的xpath路径:

下图是第二个按钮的xpath路径:

这两个路径一样的话就只能定位到一个按钮,肯定不行啦

百度说当两个按钮路径一样时,使用last()函数,使用方法:(参考教程:https://www.yiibai.com/selenium/webdriver-locating-strategies-by-xpath-using-last.html)

findElement(By.xpath("(//input[@type='text'])[last()]"))

如果页面只有两个相同地址的元素,那么第二个元素像上面这样处理就够了,代表定位最后一个元素。但是我这个页面上有四个相同地址的元素,所以就需要有选择的定位其中某个元素了,方法如下:

findElement(By.xpath("(//input[@type='text'])[last()-1]"))        注释:last()后面的-1代表定位倒数第二个,以此类推,倒数第三个就是[last()-2]

4.上传图片,点击图片上传框时报错如下:(2021-11-12)

报错的意思是“元素不可以交互”

终于找到解决方法了是元素被遮挡导致的

代码如下:

WebElement spuMain = driver.findElement(By.xpath("//*[contains(@id,'el-collapse-content')]/div/div[1]/div[1]/div/div/div[1]/div[1]/div/i"));
((JavascriptExecutor) driver).executeScript("arguments[0].click()",spuMain);

 5.页面文本定位不到(2021-12-1)

这个问题已经困扰了一天了

本来想做个断言校验一下查询出来的文本是否匹配的,结果文本死活定位不到

现在只是感觉可能和它在表格里有关系,但是层层定位路径也没问题,不知道到底是被什么影响了

2021-12-2

终于找到解决办法了

参考了这篇问答:http://cn.voidcc.com/question/p-vfkkyukq-tb.html

我之前是用的xpath路径://*[@id='searchForm']/div[2]/div[2]/div[2]/table/tbody/tr[1]/td[3]/div,放在console里验证是能找到的,可是selenium就是定位不到它

正确操作:直接定位表格里的单元格://tr[1]/td[3]

6.图片上传框和页面下拉框点击时冲突(2021-12-6)

目前的问题:

12-7发现还是因为定位的图片路径和其它路径重复冲突了,要通过last()函数区分一下

代码如下:

WebElement image = driver.findElement(By.xpath("(/html/body/div[6]/div[1]/div[1]/ul/li[2]/span)[last()]"));
JavascriptExecutor js5 = (JavascriptExecutor) driver;
js5.executeScript("arguments[0].click();", image); //因为下拉框被后面的元素遮挡了,所以要用js处理一下

 7.进入iframe后,要操作iframe外部的元素需要退出

代码如下:

driver.switchTo().defaultContent();   //退出iframe
WebElement iframe = driver.findElement(By.xpath("//*[@id='LAY_app_body']/div[2]/iframe"));
driver.switchTo().frame(iframe); //进入iframe

 8.又遇到了问题,绊住好几天了都找不到解决办法(2022-2-24)

一个用例单独执行的时候能成功执行,但是放到测试套件里和其它用例一起执行就报错找不到元素

2022-5-9上面这个问题总算找到原因了

是因为iframe路径中,div括号中的数字一直在变化,导致一直定位不到这个iframe,所以也就定位不到该页面上的元素

6-15以上这种方法的缺陷:当一个用例中包含多个iframe时,若都用这种改法,那所有iframe的路径都变成一样的了,这样就无法定位了

所以可改用下面这种通过元素中的某个属性来定位,这样就能区分开来了

 9.又遇到找不到元素的问题(2022-12-16)

UI自动化之【报错记录-selenium】的更多相关文章

  1. 【Python + Selenium】初次用IE浏览器之报错:selenium.common.exceptions.WebDriverException: Message: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones.

    初次用IE浏览器运行自动化程序时,报错:selenium.common.exceptions.WebDriverException: Message: Unexpected error launchi ...

  2. 报错记录(xml抬头报错)

    报错记录(xml抬头报错) Referenced file contains errors (http://www.springframework.org/schema/beans/spring-be ...

  3. IDEA 报错记录

    IDEA 报错记录 Process finished with exit code 0 这种主要是配了默认的 Tomcat ,然后又配置了外部的 Tomcat.解决办法,注释掉默认的: <dep ...

  4. Spring Boot 报错记录

    Spring Boot 报错记录 由于新建的项目没有配置数据库连接启动报错,可以通过取消自动数据源自动配置来解决 解决方案1: @SpringBootApplication(exclude = Dat ...

  5. selenium+python自动化93-Chrome报错:Python is likely shutting down

    遇到问题 报错信息:sys.meta_path is None, Python is likely shutting down 1.我的环境: python 3.6 selenium 2.53.6 c ...

  6. 报错记录:getOutputStream() has already been called for this response

    仅作记录:参考文章:http://www.blogjava.net/vickzhu/archive/2008/11/03/238337.html 报错信息: java.lang.IllegalStat ...

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

  8. Jmeter 接口自动化执行报错 无法找到类或者类的方法

    写好的自动化测试脚本在PC以及mac book 都执行正确,但是放到linux集成环境时就一直报错,报错类似如下 [jmeter] // Debug: eval: nameSpace = NameSp ...

  9. 小程序使用npm模块(引入第三方UI),报错的多种解决办法。

    前言引入第三方模块时,我遇到了很多坑. 首先是微信.第三方模块的文档描述不清楚.其次.搜索到的博客,大部分是抄的文档 / 相互转载抄袭.作用有限. 于是,我自己做了各种条件下的测试.解决各种情况的引入 ...

  10. g++ 6.4编译opencv-2.4.10报错记录

      fetch公司的项目进行编译,此项目依赖opencv库.由于本人一直比较偏爱fedora,但也因此给我带来了许多"乐趣"(麻烦).fedora一直走得比较前沿,g++ 6.3了 ...

随机推荐

  1. linux升级系统内核版导致死锁

    如上图片,官方说明为linux内核版本过低,存在系统bug,具体说明如下: https://baijiahao.baidu.com/s?id=1652492237858209875&wfr=s ...

  2. Cesium测量优化1

    简介:优化绘制点.线,面鼠标位置获取精度.支持3dties,gltf model,以及box等Geometry Entity上的位置拾取. 测试代码 <template> <div ...

  3. centos 6.5 docker  安装

    https://www.cnblogs.com/zhangzhen894095789/p/6641981.html?utm_source=itdadao&utm_medium=referral

  4. https代理服务器(四)java动态签发【失败】

    https://zhuanlan.zhihu.com/p/355241710?utm_id=0 http://t.zoukankan.com/xiaxj-p-8961131.html https:// ...

  5. map转listmap

    package com;import java.util.*;import java.util.stream.Collectors;public class LambadaTest { public ...

  6. ubuntu iptables 做为路由转发

    实现功能,本地服务器的号段的192.168.8.0/24,而做为路由器的机器有2个ip,192.168.8.x和另一个ip,而另一个ip可以访问 192.168.2.0/24号段, 为了让其它192. ...

  7. tomcat前后端项目部署及调优

    第1章 tomcat简介Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta项目中的一个核心项目,由Apache,Sun和其他一些公司及个人共同 ...

  8. 基于recorder.js H5录音功能

    兼容性 1.Chrome,FF,Edge,QQ,360(注:现有IE和Safari全版本不兼容) 2.其中Chrome47以上以及QQ浏览器强制要求HTTPS的支持 3.请尝试使用FF,Edge,36 ...

  9. 采集地图商家电话,导出到excel

    快速的把高德地图左边的搜索列表里的商家地图,电话,导出到EXCEL里. 采集地图商家电话,可以快速提高销售人员的业绩. 如何快速地将高德地图里的商家电话资料导出EXCEL? 操作步骤: 1. 选择你要 ...

  10. 1996. 游戏中弱角色的数量 (Medium)

    问题描述 1996. 游戏中弱角色的数量 (Medium) 你正在参加一个多角色游戏,每个角色都有两个主要属性: 攻击 和 防御 .给你一个二维整数数组 properties ,其中 properti ...