Selenium2 WebDriver Ruby =>如何单击隐藏的链接

Selenium2 WebDriver Ruby =>如何单击隐藏的链接

问题描述:

我在Ruby上使用Selenium 2 WebDriver.

I use Selenium 2 WebDriver on Ruby.

如何使用CSS单击隐藏链接(显示:无)? 该链接为子菜单,当鼠标悬停在菜单上时可见.

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

//

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

我将':id'更改为':link_text',因为子菜单没有ID. 导航:

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>

当鼠标悬停在菜单上时,您可以看到子菜单.以前没有用于webdriver的子菜单.

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

后面有代码,我在FF左下角看到menu1的链接,但是子菜单没有打开,并因超时错误而中断.

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
}

WebDriver模拟用户操作,并且不允许用户无法点击的点击元素.

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

因此,您应该执行用户会做的事情:在单击之前,将鼠标悬停在菜单上.在Ruby中,您可以执行以下操作:

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

查看更多