如何强制Selenium WebDriver点击当前不可见的元素? [英] How to force Selenium WebDriver to click on element which is not currently visible?

查看:1325
本文介绍了如何强制Selenium WebDriver点击当前不可见的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在FirefoxDriver中使用Selenium 2 Java API。
当我填写表单时,根据表单输入将复选框添加到页面中。

I am using Selenium 2 Java API with FirefoxDriver. When I fill a form, checkboxes are added to the page depending the forms inputs.

我想使用Selenium模拟这些复选框的点击。
该元素在常规浏览器中可见并可用,但是,selenium断言元素不可见。

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"

进行交互。我可以强制selenium忽略非 - 元素的可见状态?
如何强制Selenium与不可见元素交互?

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确定元素可见或不是通过以下标准(使用DOM检查器来确定什么css适用于您的元素,请确保您查看计算样式):

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(也会针对每个父元素进行检查)

  • opacity!= 0(未选中此项元素)

  • 高度和宽度都是> 0

  • 输入,属性类型!=隐藏

  • visibility != hidden
  • display != none (is also checked against every parent element)
  • opacity != 0 (this is not checked for clicking an element)
  • height and width are both > 0
  • for an input, the attribute type != hidden

您的元素符合其中一个条件。如果你没有能力改变元素的样式,那么你可以通过javascript强制执行它(自从你说Selenium2 API以来就假设WebDriver):

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);

但如果您依赖于该输入的更改事件,则不会触发javascript事件也必须解雇它(很多方法都可以这样做,最容易使用该页面上加载的任何javascript库)。

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

定义的WebDriver规范这个 -

The WebDriver spec that defines this -

https://dvcs.w3.org/hg/webdriver/raw-file/tip/webdriver-spec.html#widl-WebElement-isDisplayed-boolean

这篇关于如何强制Selenium WebDriver点击当前不可见的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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