Hover and click on hidden elements with Selenium WebDriver

3,076 views
Skip to first unread message

Muhammad Iftikhar

unread,
Aug 2, 2016, 7:51:52 AM8/2/16
to webdriver
Hi 
Hover and click on hidden elements with Selenium WebDriver

Can anyone let me know that "how to Hover and click on hidden elements with Selenium WebDriver" I tried different techniques but no one is working like http://stackoverflow.com/questions/20355515/how-to-do-a-mouse-over-using-selenium-webdriver-to-see-the-hidden-menu-without-p


Thanks,

Regards,
Iftikhar

darrell grainger

unread,
Aug 3, 2016, 8:53:50 AM8/3/16
to webdriver
I assume you are actually trying to do:

  • hover over a visible element which will cause a hidden element to appear
  • click the element which was hidden but is now visible
Clicking a hidden element isn't possible. You have to make the hidden element visible then it should be clickable. However, there is an implicit action that all humans do which you must explicitly tell Selenium to do. Being a computer program it will do LITERALLY what you tell it to do. So the REAL sequence of events is:

  • hover over a visible element which will cause a hidden element to appear
  • WAIT for the hidden element to be visible
  • click the element which was hidden but is now visible
That second step is probably what you are missing. You can usually use findElement() to find the hidden element. You just cannot click it until it is visible. Unfortunately, there is no real wait() method for Actions. You can move to an element, you can send keys to it, you can click on it but you cannot move to one element, wait, click a second element.

Sometimes I have found I can have two Actions which I can build() but not perform(). I have the first action move to the visible element to make the hidden element appear. I then build a second action which clicks the second element. Now I can do:

  • firstAction.perform();
  • wait for second element to be visible (using WebDriverWait())
  • secondAction.perform();
If this does not work then I sometimes have to break one of my rules, i.e. never use a sleep statement. The Actions class does have a deprecated pause() method. The pause() method is really just calling a Thread.sleep(). So you can do something like:

WebElement visibleElement = driver.findElement(By.cssSelector("whatever"));
WebElement hiddenElement = driver.findElement(By.cssSelector("whatever"));
Actions a = new Actions(driver);
a.moveTo(visibleElement).pause(1000).click(hiddenElement).build().perform();
 
That will work but it is not pretty and prone to breaking. 
Reply all
Reply to author
Forward
0 new messages