Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何单击SeleniumWebDriver中的隐藏元素?_Java_Html_Selenium_Selenium Webdriver - Fatal编程技术网

Java 如何单击SeleniumWebDriver中的隐藏元素?

Java 如何单击SeleniumWebDriver中的隐藏元素?,java,html,selenium,selenium-webdriver,Java,Html,Selenium,Selenium Webdriver,我有一个显示一些记录的网格。当我单击一条记录并检查该元素时,它显示为隐藏,但在网格中可见 我的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;

我有一个显示一些记录的网格。当我单击一条记录并检查该元素时,它显示为隐藏,但在网格中可见

我的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>
如果
具有id或名称,则可以使用
按id查找元素
按名称查找元素

您还可以尝试使用类名、css和xpath

find_element_by_class_name
find_element_by_css_selector
find_element_by_xpath

你有两种方法。Selenium被专门编写为不允许与隐藏元素交互。理性的观点是,如果一个人不能执行该动作,那么他也不应该这样做。因此,要通过Selenium执行单击,必须执行用户将要执行的操作,以使该按钮可见(例如,将鼠标悬停在事件上,单击另一个元素等),然后执行单击

但是,Selenium允许您在元素的上下文中执行Javascript,因此您可以编写Javascript来执行click事件,即使它是隐藏的


我的偏好是始终尝试并执行使按钮可见的操作

首先将该元素存储在对象中,比如说
元素
,然后编写以下代码以单击该隐藏元素:

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
并不总是意味着元素在DOM中隐藏或不存在,而是意味着不适合元素的溢出字符正在被修剪。基本上,这意味着即使应该显示滚动条,也不显示滚动条,因此在您的情况下,使用文本链接

塑料螺旋捆扎机

可能显示为“塑料螺旋…”或类似。所以有可能,这个链接文本确实是不存在的

因此,您可以尝试:

driver.findElement(By.partialLinkText("Plastic ")).click();
或xpath:

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

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

driver.findelement(By.xpath("//a[contains(@title, \"Plastic Spiral Bind\")]")).click();
我是用jQuery做的:

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

下面是Python中的脚本

不能单击selenium中隐藏的元素。但是,您可以执行JavaScript来为您单击隐藏的元素

element = driver.find_element_by_id(buttonID)
driver.execute_script("$(arguments[0]).click();", element)

似乎是一个常见问题:?您需要javascript。@Santhosh.S-您面临的问题是什么?是。单击()不起作用?错误的尝试…建议的答案只适用于页面上的可见元素,而不适用于隐藏元素。
溢出:隐藏不一定意味着它是隐藏的。使用伪元素创建自定义样式的收音机和复选框是您需要单击隐藏元素的另一个原因。我不明白为什么相信我的保存按钮不可见,但你用这个片段保存了(双关语)我。谢谢!我可以单击元素,即使它在屏幕上不可见。在我的情况下,这是一个幻灯片类型。按钮不可见,但存在于DOM中。(isElementDisplayed()在此场景中始终返回true)。有没有办法模拟双击?我试过点击两次,但都没用谢谢!你帮了我很多人!:)
page.execute_script %Q{ $('#some_id').prop('checked', true) }
element = driver.find_element_by_id(buttonID)
driver.execute_script("$(arguments[0]).click();", element)