webdriver报不可见元素异常方法总结
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 31 milliseconds
首先,得排除是否是定位的xpath路径有问题,如果是用xpath定位,其中用@class属性来定位,也会报这个错误(特别是class中含有复合类的定位)。下面用备份软件删除任务的弹出div区中的确认按钮定位为例:
WebElement cf_button=driver.findElement(By.xpath("//div[@class='ui-dialog-buttonset']"));
用上面的class定位,也是有问题的。用下面的路径也是有问题的,都会报上面提到的异常内容。
Xpath=//div[@class=ui-dialog-buttonpane ui-widget-content ui-helper-clearfix]/div/button
这个xpath中的class属于复合类。
如果用下面的定位,就不会报这个异常:
WebElement cf_button=driver.findElement(By.xpath("//div[4]/div[3]/div[1]/button"));
上面的元素定位,可通过元素对象的方法isDisplayed()检测元素是否可显示的,如果得到不可显示,操作该元素时会报上面提到的异常,检测语句如下:
if(!(cf_button.isDisplayed())){System.out.println("Element is not displayed!"); };
第2种情况:就是元素的样式或父级及以上元素含有不可显示属性,以致在有些浏览器中(FirefoxDriver)不能操作,但在正常的浏览器中它是可见和可用的。那么需要检查元素是否具备如下特性:
- visibility!= hidden
- display != none (is also checked against every parent element)
- opacity != 0 (in rc2 this is no longer checked for clicking an element)
- height and width are both > 0
- for an input, the attribute type != hidden
如果有不符上面的特性之一,那么就用js脚本来处理,这个解决方案的原英文附在下面:
Element is not currently visible
I am using Selenium 2.0b3 Java API with FirefoxDriver. When I fill a form, checkboxes are added to the page depending the forms inputs. I to simulate a click on those checkboxes using selenium. The element are visible and usable in a regular browser but, selenium asserts that the elements are not visible.
"Element is not currently visible and so may not be interacted with"
Can I force selenium to ignore the non-visible state of the elements? How can I force selenium to interact with the non visible element?
Selenium determines an element is visible or not by the following criteria (use a DOM inspector to determine what css applies to your element, make sure you look at computed style):
- visibility != hidden
- display != none (is also checked against every parent element)
- opacity != 0 (in rc2 this is no longer checked for clicking an element)
- height and width are both > 0
- for an input, the attribute type != hidden
Your element is matching one of those criteria. If you do not have the ability to change the styling of the element, here is how you can forcefully do it with javascript. (going to assume WebDriver since you said Selenium2 API):
((JavascriptExecutor)driver).executeScript("arguments[0].checked = true;", inputElement);
But that won't fire a javascript. event, if you depend on the change event for that input you'll have to fire it too (many ways to do that, easiest to use whatever javascript. library is loaded on that page).
第3种情况是:估计意思是就是选取元素的时候,可能存在两个相同的,但一次只会用一个,这两个区别就有一个样式的属性是不可见的,这个时候选取元素时要去掉不可见属性,如下例子:
WebElement label = driver.findElement(By.xpath("//
label[text()='User Name:' and not(contains(@style,'display:
none'))]"));
这个源版的这种情况下的解决方案附在下面:
can think of two reasons the element you are attempting to interact
with is invisible. The first reason is that some fields might be
invisible until you fill in other fields. For example, many
registration forms have a submit button which is not visible until the
user has filled in all the required fields. WebDriver is designed to
simulate a human interacting with the web page. Are you filling out
the page exactly the same way a human would? Maybe you filled in all
the fields but your automation is faster than a human. So it tries to
click the submit button before the web page can change it to visible.
You can use things like http://darrellgrainger.blogspot.ca/2012/01/waitforelement.html
to help slow down the automation.
The second reason the element might be invisible is you have located
an invisible element rather than the element you thought you wanted.
For example, I worked on a web page which had a radio button for new
user versus sign in. If you clicked new user, the form. for registering
a new user appeared. If you clicked existing user, the form. for
signing in appeared. Both forms existed in the DOM but only one was
visible at a time. Both forms had:
<label for="foo29_blah_nix17">User Name:</label>
<input id="foo29_blah_nix17" />
(the for/id values would be different). So if I used:
String username = "darrell";
WebElement label = driver.findElement(By.xpath("//
label[text()='User Name:']"));
String inputID = label.getAttribute("for");
WebElement input = driver.findElement(By.id(inputID));
input.sendKeys(username)
it might find the invisible label. In these cases, I need to use an
xpath which finds the visible label element. If the element is
invisible because style='display: none' then I'd use:
WebElement label = driver.findElement(By.xpath("//
label[text()='User Name:' and not(contains(@style,'display:
none'))]"));
You'll have to figure out if you have problem #1 or #2. If you have
problem #2, you will have to figure out the exact solution. My
solution is just one of many possible examples and might not be
exactly your problem.
第4种情况:一般是编写脚本流程没有严格按照手工的步骤来进行,导致选取到非流程中的不可显示元素,进行操作,以致报上面提到的异常,这种情况在第3种情况的首段有所描述。
webdriver报不可见元素异常方法总结的更多相关文章
- python webdriver api-操作日期元素的方法
操作日期元素 第一种方式直接向输入框输入日期 dateInputBox = self.driver.find_element_by_id("datepicker") dateInp ...
- Selenium之WebDriver元素定位方法
Selenium WebDriver 只是 Python 的一个第三方框架, 和 Djangoweb 开发框架属于一个性质. webdriver 提供了八种元素定位方法,python语言中也有对应的方 ...
- WebDriver元素查找方法摘录与总结
WebDriver元素查找方法摘录与总结 整理By:果冻迪迪 selenium-webdriver提供了强大的元素定位方法,支持以下三种方法. • 单个对象的定位方法 • 多个对象的定位方法 • 层级 ...
- webdriver中定位元素,报无法找到元素的问题
webdriver中定位元素,报无法找到元素的问题时,需要查看以下几点: 1 用火狐的firebug插件定位元素,确保这个元素的定位正确: 2 在火狐的firebug插件的,在html页签中输入fra ...
- webDriver定位元素的方法
在UI层面的自动化测试开发中,元素的定位与操作是基础,也是经常遇到的困难所在.webdriver提供了8种定位: 1. id定位:find_element_by_id("id值") ...
- Selenium2(WebDriver)总结(三)---元素定位方法
元素定位的重要性不言而喻,如果定位不到元素谈何操作元素呢,webdrvier提供了很多种元素定位方法,如ID,Name,xpath,css,tagname等. 例如需要定位如下元素: <inpu ...
- 我的Java开发学习之旅------>Java使用ObjectOutputStream和ObjectInputStream序列号对象报java.io.EOFException异常的解决方法
今天用ObjectOutputStream和ObjectInputStream进行对象序列化话操作的时候,报了java.io.EOFException异常. 异常代码如下: java.io.EOFEx ...
- spring事务(Transaction )报 marked as rollback-only异常的原因及解决方法
很多朋友在使用spring+hibernate或mybatis等框架时经常遇到报Transaction rolled back because it has been marked as rollba ...
- 强制等待&隐士等待&显示等待&元素定位方法封装
前言 问题 学习selenium的同学估计大多数都遇见过一个问题 明明页面已经精准的定位到了元素,但是执行脚本的时候却经常报错没找到元素.其实原因很简单,就是脚本执行的速度很快,而浏览器加载页面的时候 ...
随机推荐
- 【公有云】在阿里云中申请免费ssl证书
准备 拥有阿里云账号 拥有域名,最好是在同个账号下,方便操作. 申请证书 第一步:进入申请 第二步:选择证书类型 第三步:支付,就是走个流程,不用给钱 第四步:填写证书信息 第五步:验证域名 第六步: ...
- 基于Java+Selenium的WebUI自动化测试框架(二)-----页面操作接口
在有了基础的Position类之后,我们需要考虑我们在寻找完页面元素之后,需要做什么.这个“做”什么,可以理解为我们在页面上需要对应的一系列动作.比如:点击,输入,切换窗口,寻找元素,判断元素是否存在 ...
- 大数据之路week07--day04 (Linux 中查看文件内容的关键字处)
Linux如何对文件内容中的关键字进行查找 如果是用vi打开文件后,在命令行下输入“/关键字” 如果是在没有打开文件的前提就用"cat 文件名 | grep "关键字" ...
- 0022SpringMVC解决post请求中文乱码的问题
我们在页面难免提交一些中文数据给后台处理,但是发现后台拿到的数据乱码,可以在每一个controller中都设置编码,但是太过于麻烦, 正确的解决办法应该是在web.xml中配置解决中文乱码的过滤器: ...
- 前端学习笔记--CSS样式--背景和超链接
1.背景 2.超链接: 举例:
- Mybatis-Generator逆向工程,复杂策略(Criteria拼接条件)
基于上一篇修改 1.Generator配置文件修改,将targetRuntime改为MyBatis3 2.项目结构目录 这个xxxExample就是拼接条件用的 3.测试代码 注释写的很详细 publ ...
- redis--基于内存的高速缓存,NoSql的典型代表
NoSql入门和概述 入门概述 为什么要使用NoSql? 1.单机mysql的美好年代 在早些年以前,那时候网站的访问量不大,用单个数据库完全可以应付.而且那个时候,绝大部分都是LAMP架构:Linu ...
- AtCoder Beginner Contest 143 F - Distinct Numbers
题意 给出一个长度为NNN的序列,求对于所有k∈[1,N]k\in[1,N]k∈[1,N],每次从序列中选出kkk个互不相同的数,最多能取多少次. N≤3e5N\le3e5N≤3e5 题解 我们首先把 ...
- [NOI2013]快餐店 / CF835F Roads in the Kingdom (基环树)
题意 一颗基环树,选一对点使得这两个点的最短距离最大. 题解 相当于找基环树的直径,但是这个直径不是最长链,是基环树上的最短距离. 然后不会做. 然后看了ljh_2000的博客. 然后会了. 这道题最 ...
- Mybatis延迟加载, 一级缓存、二级缓存
延迟加载 概念:MyBatis中的延迟加载,也称为懒加载,是指在进行关联查询时,按照设置延迟规则推迟对关联对象的select查询.延迟加载可以有效的减少数据库压力. (注意:MyBatis的延迟加载只 ...