元素当前不可见,因此可能无法与之交互 [英] Element is not currently visible and so may not be interacted with

查看:23
本文介绍了元素当前不可见,因此可能无法与之交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在尝试解析示例站点.在该站点中有一个设置为显示:无"为了应用css效果.我的意思是实际上选择组件可见.

Currently i am trying to parse a sample site. In that site there is a which set to "display :none" in order to apply css effects. I mean in fact that select component is visible.

如何为该 html select 组件设置值?

How can i set value to that html select component ?

这是我的代码:

 WebElement element = driver.findElement(By.name("paper"));
 Select paperList = new Select(element);
 paperList .selectByValue("73"); // it gives error
 driver.findElement(By.className("btn1")).click();

我使用的是 Java + Selenium WebDriver 2.0

I am using Java + Selenium WebDriver 2.0

这是个例外:

引起:org.openqa.selenium.remote.ErrorHandler$UnknownServerException: 元素当前不可见,因此可能不与构建信息交互:版本:'2.14.0',修订:'未知',时间:'2011-11-29 13:13:16' 系统信息:os.name:'WindowsXP', os.arch: 'x86', os.version: '5.1',​​ java.version: '1.7.0-ea'

Caused by: org.openqa.selenium.remote.ErrorHandler $UnknownServerException: Element is not currently visible and so may not be interacted with Build info: version: '2.14.0', revision: 'unknown', time: '2011-11-29 13:13:16' System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.7.0-ea'

谢谢.

推荐答案

好吧,我能想到有两种不同的方法来处理这个问题.首先清理页面上的css,这样就不会在同一个元素上出现两种冲突的样式.其次,我的猜测是您有某种形式的 javascript 启用了选择菜单,该菜单可能在您的 selenium 测试抓取对象并说它不可见后触发.在获取代码之前尝试删除 thread.sleep,仅用于测试目的.如果这解决了问题,那么我将创建一个 javascript 来查找 document.readyState,如果它完整",那么我将开始使用 Selenium 解析页面.

Well there are two different ways to handle this that I can think of. First clean up the css on the page so that you don't have two conflicting styles on the same element. Second my guess is you have some form of javascript enabling the select menu which is probably firing after your selenium test grabs the object and says that it's not visible. Try dropping a thread.sleep before you grab the code, just for testing purposes. If that resolves the issue then I would create a javascript that would look for document.readyState and if it were "complete" then I would start parsing the page with Selenium.

------ 编辑------

------ EDIT ------

要使用 selenium 修改 DOM,您需要使用 JavascriptExecutor 类并将其转换为您的 WebDriver.执行此操作后,您可以从页面上的 selenium 应用程序执行 javascript,这意味着您可以操作 DOM.

Well to modify the DOM with selenium you need to use the JavascriptExecutor class and cast it to your WebDriver. After you do that then you can execute javascript from your selenium application on the page which means you can manipulate the DOM.

这是一个例子:

WebDriver driver; // Assigned elsewhere
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('//elementID').removeAttr('display');");

http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions

http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/JavascriptExecutor.html

这篇关于元素当前不可见,因此可能无法与之交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆