简体   繁体   English

如何单击 Selenium WebDriver 中的隐藏元素?

[英]How to click on hidden element in Selenium WebDriver?

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

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

You have two approaches.你有两种方法。 Selenium has been specifically written to NOT allow interaction with hidden elements. Selenium 已被专门编写为不允许与隐藏元素交互。 The rational is that if a person cannot perform that action, then neither should Selenium.理由是,如果一个人不能执行该操作,那么 Selenium 也不应该执行该操作。 Therefore, to perform the click via Selenium, you must perform the action a user would do to make that button visible (eg mouse over event, click another element, etc) then perform the click once visible.因此,要通过 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我的偏好是始终尝试执行使按钮可见的操作

Here is the script in Python.这是 Python 中的脚本。

You cannot click on elements in selenium that are hidden.您不能单击 selenium 中隐藏的元素。 However, you can execute JavaScript to click on the hidden element for you.但是,您可以执行 JavaScript 来为您单击隐藏的元素。

element = driver.find_element_by_id(buttonID)
driver.execute_script("$(arguments[0]).click();", element)
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.并不总是意味着元素在 DOM 中隐藏或不存在,这意味着不适合元素的溢出字符正在被修剪。 Basically it means that do not show scrollbar even if it should be showed, so in your case the link with text基本上这意味着即使应该显示滚动条也不显示滚动条,因此在您的情况下,带有文本的链接

Plastic Spiral Bind塑料螺旋装订

could possibly be shown as "Plastic Spir..." or similar.可能会显示为“Plastic Spir...”或类似内容。 So it is possible, that this linkText indeed is non existent.所以有可能,这个linkText确实不存在。

So you can probably try:所以你可能可以尝试:

driver.findElement(By.partialLinkText("Plastic ")).click();

or xpath:或 xpath:

//a[contains(@title, \"Plastic Spiral Bind\")]

我用jQuery做到了:

page.execute_script %Q{ $('#some_id').prop('checked', true) }

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_idfind_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

通过使用 Selenium IDE 来使用链接的 XPath 单击元素:

driver.findelement(By.xpath("//a[contains(@title, \"Plastic Spiral Bind\")]")).click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM