如何强制 Selenium WebDriver 单击当前不可见的元素?
我正在使用 Selenium 2 Java API 和 FirefoxDriver。 当我填写表单时,复选框会根据表单输入添加到页面中。
我想使用 Selenium 模拟点击这些复选框。 该元素在常规浏览器中可见且可用,但 selenium 断言该元素不可见。
"Element is not currently visible and so may not be interacted with"
我可以强制硒忽略元素的不可见状态吗? 如何强制 Selenium 与不可见元素相互作用?
I am using Selenium 2 Java API with FirefoxDriver.
When I fill a form, checkboxes are added to the page depending the forms inputs.
I'd like 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
Selenium 通过以下标准确定元素是否可见(使用 DOM 检查器来确定哪些 css 适用于您的元素,确保查看计算样式):
您的元素与这些条件之一匹配。如果您没有能力更改元素的样式,则可以使用以下方法强制使用 javascript 进行更改(由于您提到了 Selenium2 API,因此将假设为 WebDriver):
但是,如果您依赖,则不会触发 javascript 事件在该输入的更改事件上,您也必须触发它(有很多方法可以做到这一点,最简单的是使用该页面上加载的任何 javascript 库)。
可见性检查的来源 -
https://github。 com/SeleniumHQ/selenium/blob/master/javascript/atoms/dom.js#L577
定义此的 WebDriver 规范 -
https://dvcs.w3 .org/hg/webdriver/raw-file/tip/webdriver-spec.html#widl-WebElement-isDisplayed-boolean
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):
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):
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).
The source for the visibility check -
https://github.com/SeleniumHQ/selenium/blob/master/javascript/atoms/dom.js#L577
The WebDriver spec that defines this -
https://dvcs.w3.org/hg/webdriver/raw-file/tip/webdriver-spec.html#widl-WebElement-isDisplayed-boolean
有时,这意味着页面上有多个元素具有您尝试搜索的相同属性,并且您正在“与错误的元素交谈”。
如果您的元素无法通过 :id 或 :name (或 :class)唯一标识,则可能会很棘手。
有时通过 :xpath 搜索元素会有所帮助,但在某些情况下甚至这是不切实际的。
在这些情况下,您可能必须获取符合您的条件的所有元素,并通过索引引用正确的元素。它很脏,但很管用。
我正在使用 Ruby on Rails 应用程序中的 Selenium / Watir,所以在我的例子中,示例是:
希望这会有所帮助。
Sometimes this means there are multiple elements on a page that have the same property you're trying to search by and you're "talking to the wrong one".
If your element can't be uniquely identified by :id or :name (or :class), it could be tricky.
Sometimes searching for the element by the :xpath will help and in some cases even that is not practical.
In those cases, you may have to get all the elements that match your criteria and reference the right one by the index. It's dirty, but it works.
I'm using Selenium / Watir from Ruby on Rails app, so in my case the example would be:
Hope this helps.
我有一个类似的问题,但它与元素在视口中不可见有关。我截取了屏幕截图,发现浏览器窗口太窄,看不到该元素。我做了其中之一,它起作用了:
请参阅: WebDriver .maximize_window()
I had a similar issue, but it was related to the element not being visible in the viewport. I took a screenshot and realized the browser window was too narrow and the element couldn't be seen. I did one of these and it worked:
See: WebDriver.maximize_window()
或者您可以使用 Selenium Action Class 来模拟用户交互 -
例如
Or you may use Selenium Action Class to simulate user interaction --
For example
还有另一种情况,可见元素将被识别为不可见:
为了检查您不想与之交互的元素是否经过 CSS 转换,在 CHROME 上执行以下操作: this:
There is also another case when visible element will be recognized as not visible:
In order to check if element you wan't to interact with is CSS transformed, on CHROME do this:
不可见也可能是由于元素应该慢慢出现的时间造成的。
在这种情况下,强制浏览器稍等一下可能会有所帮助。
例如,请参阅让 WebDriver 等待元素出现的问题存在。
The invisibility can also be due to timing when the element is supposed to slowly appear.
Forcing the browser to wait a bit might help in that case.
See, e.g., the question on letting WebDriver wait until an element is present.
此页面上有很多很好的答案。
driver Factory 或任何初始化驱动程序的地方。这是默认执行的操作 - 始终如此。
上面对两者进行了各种详细讨论。我没有看到的答案是 ScrollToElement。听起来您正在处理元素列表,而在处理时您正在创建更多元素、复选框。这可能会导致列表中的元素移出可见页面。有时您可以用肉眼看到该元素,但就是无法单击它。处理列表时,有时必须插入滚动。
窗口边缘、右上/左下。有时当这是
如果您无法通过 selenium 访问它,但您可以手动单击
你的鼠标。
因为我遇到过这个问题,所以我创建了一个 PageScroll.java 并将滚动脚本放在那里。以下是此类中的一些方法:
请参阅 使用 Selenium 将元素滚动到视图中
获取更多示例
There are many good answers on this page.
driver Factory or wherever you initialize your driver. This is something done by default - always.
Both are discussed in various details above. The answer I didn't see was ScrollToElement. It sounds like you are processing a list of elements, while processing you are creating more elements, checkboxes. This can cause elements in your list to move off the visible page. Sometimes you can see the element with the naked eye but you just can't click on it. When processing lists you sometimes have to interject scrolling.
the window edge, top/bottom right/left. Sometimes when this is the
case you can't get to it via selenium but you can manually click with
your mouse.
Because I run across this I created a PageScroll.java and put my scrolling scripts there. Here are a few of the methods from this class:
see Scroll Element into View with Selenium
for more examples
我在 Internet Explorer 9 中使用 selenium 2 也遇到了同样的问题,但我的修复方法确实很奇怪。我无法点击表单内的输入 ->硒重复,它们不可见。
当我的形体有弯曲的阴影时就会发生这种情况 -> http://www.paulund.co.uk/creating- Different -css3-box-shadows-effects:在具体的“效果2”中
,我不知道为什么以及如何这个伪元素解决方案停止了硒测试,但它对我有用。
I had the same problem with selenium 2 in internet explorer 9, but my fix is really strange. I was not able to click into inputs inside my form -> selenium repeats, their are not visible.
It occured when my form had curved shadows -> http://www.paulund.co.uk/creating-different-css3-box-shadows-effects: in the concrete "Effect no. 2"
I have no idea, why&how this pseudo element solution's stopped selenium tests, but it works for me.
您不能强制访问/更改用户通常无权访问的元素,因为 Selenium 旨在模仿用户交互。
如果发生此错误,请检查:
在node.js中最大化()
,maximize_window()
in Python),
executeScript()
在 node.js 中,execute_script()
(Python)。You can't force accessing/changing element to which the user normally doesn't have access, as Selenium is designed to imitate user interaction.
If this error happens, check if:
maximize()
in node.js,maximize_window()
in Python),executeScript()
in node.js,execute_script()
in Python).我只是通过等待元素显示属性来解决这个错误
I just solve this error by waiting element display property
我在 django 站点上使用 selenium 进行功能测试时遇到 ElementNotVisibleException 异常,其中引导标志符号作为模板中的链接:
在我的功能测试期间,引导样式未加载,然后尝试单击此类链接将引发 ElementNotVisibleException。我设法使它们可点击只需在标签中添加一个空格,如下所示:
I get into ElementNotVisibleException exception using selenium for functional tests on a django site with bootstrap glyphicons as links in templates as:
During my functional tests, the bootstrap style is not loaded, then trying to click on such links will raise ElementNotVisibleException. I manage to make them clickable just adding a space in the tag, like that:
在这里添加我的沙粒:如果一个元素位于固定 div 后面,它将被视为不可见,并且您将无法单击它;这件事最近发生在我身上,我按照上面的建议执行一个脚本解决了这个问题,它的作用是:
To add my grain of sand here: if an element resides behind a fixed div it will be considered as not visible and you won't be able to click it; this happened to me recently and i solved it executing a script as recommended above, which does:
我只是在 ror 项目中使用水豚时解决了这个错误,方法是添加:
到features/support/env.rb。
I just solve this error while using capybara in ror project by adding:
to
features/support/env.rb
.接受的答案对我有用。下面是一些代码,用于查找使 Selenium 认为它不可见的父元素。
The accepted answer worked for me. Below is some code to find the parent element that's making Selenium think it's not visible.