Javascript Selenium WebDriver 单击隐藏元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12040615/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:57:41  来源:igfitidea点击:

Selenium WebDriver clicking on hidden element

javascriptseleniumselenium-webdriverwebdriverhidden-field

提问by Chun ping Wang

Hi I would like to know how to click on hidden element and/or disable element by using Selenium WebDriver.

嗨,我想知道如何使用 Selenium WebDriver 单击隐藏元素和/或禁用元素。

I know with selenium 1 I can do this as below:

我知道使用 selenium 1 我可以按如下方式执行此操作:

selenium.click(id="idOfHiddenField");

and this would work, but with selenium 2 (WebDriver), this doesn't. I do not want to use jquery to enable or show hidden fields , or JavaScript. This is because most of the test are using xpath.

这会起作用,但是使用 selenium 2 (WebDriver),则不起作用。我不想使用 jquery 来启用或显示隐藏字段或 JavaScript。这是因为大部分测试都使用 xpath。

Or do I just have to stay with old selenium which allows you to click on hidden fields?

或者我是否只需要使用旧的 selenium 来让你点击隐藏的字段?

回答by Ashwin Prabhu

There is a easier way to work around the problem using JavascriptExecutor.

有一种更简单的方法可以使用JavascriptExecutor.

For example:

例如:

document.getElementsByClassName('post-tag')[0].click();

The above javascript would click on the "Selenium" tag on the top right of this page (next to your question), even if it were hidden (hypothetically).

上面的 javascript 会点击此页面右上角的“Selenium”标签(在您的问题旁边),即使它被隐藏(假设)。

All you need to do is issue this JS instruction via the JavascriptExecutorinterface like so:

您需要做的就是通过JavascriptExecutor接口发出这个 JS 指令,如下所示:

(JavascriptExecutor(webdriver)).executeScript("document.getElementsByClassName('post-tag')[0].click();");

This would use the JS sandbox and synthetic click event to perform the click action. Although it defeats the purpose of WebDriver user activity simulation, you can use it in niche scenarios like in your case to good effect.

这将使用 JS 沙箱和合成点击事件来执行点击操作。虽然它违背了 WebDriver 用户活动模拟的目的,但您可以在像您的案例这样的小众场景中使用它,以取得良好的效果。

回答by Prashant Shukla

there is one answer but multiple suggestions:

有一个答案,但有多种建议:

Answer: use selenium backed driver to click on the hidden element using something like

答:使用 selenium 支持的驱动程序使用类似的东西点击隐藏元素

Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
selenium.clickAt("xpath=//area[@alt='Mercury']", clickPoint);

Suggestion 1: to create fake data specially if there's a lot to be created and you are looking for FOSS go for JMeter.

建议1:如果要创建的数据很多并且您正在寻找FOSS,请特别创建假数据,请选择JMeter。

Suggestion2: to check javascript turned off disable javascript in the firefox instance itself e.g.

建议2:检查javascript关闭在firefox实例本身中禁用javascript例如

DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setJavascriptEnabled(false);

回答by Wil

It may not fit your needs, but another solution for some might be to alter the CSS for hiding an element.

它可能不适合您的需求,但对于某些人来说,另一种解决方案可能是更改 CSS 以隐藏元素。

Whilst Selenium won'tfind an element with display: none;it willfind an element with the following:

虽然 Selenium不会找到一个元素,display: none;但它找到一个具有以下内容的元素:

.hide {
    position: absolute;
    top: -99em;
    left: -99em;
}

Then rather than using jQuery.show/hide/togglein your app, you would use jQuery.toggleClass('hide', true/false/unset_to_toggle).

然后,而不是jQuery.show/hide/toggle在您的应用程序中使用,您将使用jQuery.toggleClass('hide', true/false/unset_to_toggle).