Selenium2 WebDriver Ruby =>;如何点击隐藏链接

发布于 2024-11-25 22:39:13 字数 1003 浏览 1 评论 0原文

我在 Ruby 上使用 Selenium 2 WebDriver。

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

//编辑:

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

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

<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 的子菜单不存在。

使用以下代码,我在 FF 左下角看到了来自 menu1 的链接,但子菜单未打开并因超时错误而中断。

menu = @driver.find_element(:link_text => "menu")
@driver.action.move_to(menu).perform
wait.until {
  @driver.find_element(:link_text => "submenu").click
}

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

近箐 2024-12-02 22:39:13

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

因此,您应该像用户一样:将鼠标悬停在菜单上,然后单击。在 Ruby 中,您可以执行以下操作:

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

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

ActionBuilder 类(由 Driver#action 返回)记录在 此处

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.

北方。的韩爷 2024-12-02 22:39:13

在搜索链接并单击它之前,

@driver.execute_script('jQuery(":hidden","#nav-main").show()')

应该加载 jQuery

before you search the link and click it

@driver.execute_script('jQuery(":hidden","#nav-main").show()')

jQuery should be loaded

故事↓在人 2024-12-02 22:39:13

在过去,如果我对 ActionBuilder 方法没有太多运气,我只是使用纯 javascript:

def mouse_over_and_out(element)
  @driver.execute_script("if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}", element)
  yield element
  @driver.execute_script("if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseout', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseout');}", element)
end

def wait_for_visibility(element) 
  wait = Selenium::WebDriver::Wait.new(:timeout => 15)
  wait.until do
    element.visible?
  end
  element
end

mouse_over_and_out(@driver.find_element(:css => "ul#nav-main li")) do |menu|
  submenu = menu.find_element(:css => "ul li a")
  wait_for_visibility(submenu).click
end

In the past, if I have not had much luck with the ActionBuilder methods, I just use pure javascript:

def mouse_over_and_out(element)
  @driver.execute_script("if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}", element)
  yield element
  @driver.execute_script("if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseout', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseout');}", element)
end

def wait_for_visibility(element) 
  wait = Selenium::WebDriver::Wait.new(:timeout => 15)
  wait.until do
    element.visible?
  end
  element
end

mouse_over_and_out(@driver.find_element(:css => "ul#nav-main li")) do |menu|
  submenu = menu.find_element(:css => "ul li a")
  wait_for_visibility(submenu).click
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文