Java 如何单击 Selenium WebDriver 中的隐藏元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22110282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to click on hidden element in Selenium WebDriver?
提问by Santhosh S
I have a grid which displays some records. When I click on a record and inspect that element it is shown that it is hidden but it is visible in the grid.
我有一个显示一些记录的网格。当我单击一条记录并检查该元素时,它会显示它是隐藏的,但它在网格中是可见的。
My HTML is:
我的 HTML 是:
<a href="http://192.168.1.6/eprint_prod_3.8/settings/othercost_add.aspx?type=edit&id=805" title="Plastic Spiral Bind"
<div style="float: left; width: 99%; overflow: hidden; height: 15px; overflow: hidden"> Plastic Spiral Bind </div>
</a>
The above code is hidden while inspecting but it is visible in grid.
上述代码在检查时隐藏,但在网格中可见。
Selenium code:
硒代码:
driver.findElement(By.partialLinkText("Plastic Spiral Bind")).click();
采纳答案by TDHM
First store that element in object, let's say element
and then write following code to click on that hidden element:
首先将该元素存储在对象中,假设element
然后编写以下代码以单击该隐藏元素:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
回答by user2118784
If the <div>
has id or name then you can use find_element_by_id
or find_element_by_name
如果<div>
有 id 或 name,那么您可以使用find_element_by_id
或find_element_by_name
You can also try with class name, css and xpath
您也可以尝试使用类名、css 和 xpath
find_element_by_class_name
find_element_by_css_selector
find_element_by_xpath
回答by Robbie Wareham
You have two approaches. Selenium has been specifically written to NOT allow interaction with hidden elements. The rational is that if a person cannot perform that action, then neither should Selenium. Therefore, to perform the click via Selenium, you must perform the action a user would do to make that button visible (e.g mouse over event, click another element, etc) then perform the click once visible.
你有两种方法。Selenium 已被专门编写为不允许与隐藏元素交互。理由是,如果一个人不能执行该操作,那么 Selenium 也不应该执行该操作。因此,要通过 Selenium 执行单击,您必须执行用户将执行的操作以使该按钮可见(例如鼠标悬停事件、单击另一个元素等),然后在可见后执行单击。
However, Selenium does allow you to execute Javascript within the context of an element, so you could write Javascript to perform the click event even if it is hidden.
但是,Selenium 确实允许您在元素的上下文中执行 Javascript,因此您可以编写 Javascript 来执行单击事件,即使它是隐藏的。
My preference is to always try and perform the actions to make the button visible
我的偏好是始终尝试执行使按钮可见的操作
回答by Erki M.
overflow:hidden
does not always mean that the element is hidden or non existent in the DOM, it means that the overflowing chars that do not fit in the element are being trimmed. Basically it means that do not show scrollbar even if it should be showed, so in your case the link with text
并不总是意味着元素在 DOM 中隐藏或不存在,这意味着不适合元素的溢出字符正在被修剪。基本上这意味着即使应该显示滚动条也不显示滚动条,因此在您的情况下,带有文本的链接
Plastic Spiral Bind
塑料螺旋装订
could possibly be shown as "Plastic Spir..." or similar. So it is possible, that this linkText indeed is non existent.
可能会显示为“Plastic Spir...”或类似内容。所以有可能,这个linkText确实不存在。
So you can probably try:
所以你可能可以尝试:
driver.findElement(By.partialLinkText("Plastic ")).click();
or xpath:
或 xpath:
//a[contains(@title, \"Plastic Spiral Bind\")]
回答by Bhagyashrip
Use an XPath of the link by using Selenium IDE to click the element:
通过使用 Selenium IDE 来使用链接的 XPath 单击元素:
driver.findelement(By.xpath("//a[contains(@title, \"Plastic Spiral Bind\")]")).click();
回答by aarkerio
I did it with jQuery:
我用jQuery做到了:
page.execute_script %Q{ $('#some_id').prop('checked', true) }
回答by Peter
Here is the script in Python.
这是 Python 中的脚本。
You cannot click on elements in selenium that are hidden. However, you can execute JavaScript to click on the hidden element for you.
您不能单击 selenium 中隐藏的元素。但是,您可以执行 JavaScript 来为您单击隐藏的元素。
element = driver.find_element_by_id(buttonID)
driver.execute_script("$(arguments[0]).click();", element)