11

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

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

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.

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

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Chun ping Wang
  • 3,879
  • 12
  • 42
  • 53
  • 1
    WebDriver does not allow for interacting with hidden elements. It makes sense - user cannot click a hidden field so why your tests should be doing so? – JacekM Aug 20 '12 at 17:13
  • Like JaceM mentioned go through the web flow like a normal user would. If a user has to click something to make that element visible then do it. There isn't much more I can say on this matter. – Greg Aug 20 '12 at 17:57
  • i want to be able to create fake data on test environment but through hidden field hacks.. also test against javascript turned off... etc. – Chun ping Wang Aug 20 '12 at 20:29
  • A lot of jquery-ui widgets use hidden elements, which contain the real value being submitted. Those jquery-ui widgets can be changed out, breaking all your tests, but if you test using the underlying element, your tests will continue to work. I'm thinking CSS injection might be a way to force all the desired hidden elements visible. But the test might look funny having two visible elements side-by-side for every jquery-ui widget. – Brain2000 Sep 27 '18 at 17:49

3 Answers3

16

There is a easier way to work around the problem using 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).

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

(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.

Ashwin Prabhu
  • 9,285
  • 5
  • 49
  • 82
  • thanks, but most of them i have used xpath like //input[@value='xxx'] – Chun ping Wang Aug 21 '12 at 15:26
  • 3
    That's not by point. Any locator would locate any hidden element, XPath, CSS, id or whatever. I was suggesting you use JSExecutor to bypass WebDriver smartness. Did you try it? Did it work for you? You can continue using XPath locator to locate a WebElement and pass it into the JS script which will click the passed-in element. – Ashwin Prabhu Aug 22 '12 at 05:40
1

there is one answer but multiple suggestions:

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

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.

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

DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setJavascriptEnabled(false);
Prashant Shukla
  • 1,391
  • 9
  • 18
0

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

Whilst Selenium won't find an element with display: none; it will find an element with the following:

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

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

Wil
  • 4,887
  • 3
  • 22
  • 30