Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium2 WebDriver Ruby => how click on a hidden link

I use Selenium 2 WebDriver on Ruby.

How it is possible click on hidden link, with css (display: none)? the link is submenu and is visible when mouse over on menu.

//EDIT:

Selenium::WebDriver::Error::NoSuchElementError: Unable to locate element: {"method":"link text","selector":"submenu2"}

I changed ':id' to ':link_text', because the submenu have no id's. the Navigation:

<ul id="nav-main">
 -<li class="menu active">
    <p>
      <a href="/menu1">menu1</a>
    </p>
   -<ul> <-- begin display:none
     -<li>
        <p>
          <a href="/submenu1">submenu1</a>
        </p>
      </li>
     +<li>
    </ul> <--end submenu
  </li>
</ul>

you can see the submenu, when mouseover menu. Before are the submenu for webdriver not exist.

with followed code I see the link from menu1 in FF left-bottom, but the submenu is not opened and break with a timeout error.

menu = @driver.find_element(:link_text => "menu")
@driver.action.move_to(menu).perform
wait.until {
  @driver.find_element(:link_text => "submenu").click
}
like image 841
Andrej Avatar asked Jul 21 '11 14:07

Andrej


People also ask

How do you click on an element which is hidden using selenium Webdriver?

We can click on an element which is hidden with Selenium webdriver. The hidden elements are the ones which are present in the DOM but not visible on the page. Mostly the hidden elements are defined by the CSS property style="display:none;".

How does selenium handle hidden links?

Selenium by default cannot handle hidden elements and throws ElementNotVisibleException while working with them. Javascript Executor is used to handle hidden elements on the page. Selenium runs the Javascript commands with the executeScript method. The commands to be run are passed as arguments to the method.


1 Answers

WebDriver emulates user actions, and doesn't allow clicking elements that a user wouldn't be able to click.

So you should do what a user would do: mouse over the menu before clicking. In Ruby you could do e.g.:

menu = driver.find_element(:id => "menu")
submenu = driver.find_element(:id => "submenu")

driver.action.move_to(menu).click(submenu).perform

The ActionBuilder class (returned by Driver#action) is documented here.

like image 95
jarib Avatar answered Nov 28 '22 16:11

jarib