如何用水豚模拟鼠标悬停 [英] How to emulate mouse hover with Capybara

查看:51
本文介绍了如何用水豚模拟鼠标悬停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想做的是单击一个在悬停另一个元素(其父元素)时可见的按钮。

Basically, what I'm trying to do is click on a button that becomes visible when hovering another element (its parent).

我尝试使用 trigger。('mouseover'),但似乎无效。

I have tried to use trigger.('mouseover') on the parent of the hidden button, but that doesn't seem to work.

以下是该规范的代码段:

Here's a code snippet from the spec:

 # label[for ... ] -> the parent element
 page.execute_script("$('label[for=\"department_#{department.id}\"]').trigger(\"mouseover\")")     
 # le hidden button
 find(".actions").click     
 # some <li> on a list that drops down when clicking the hidden button    
 click_on("Edit department")

和错误...

 Failure/Error: click_on("Edit department")
 Selenium::WebDriver::Error::ElementNotVisibleError:
 Element is not currently visible and so may not be interacted with

我想知道如何在页面上显示 .actions 按钮,以便以后单击它。

I would like to know how can I make the .actions button visible on the page, in order to click it afterwards.

任何帮助将不胜感激。

推荐答案

Capybara提供了 Element#hover 方法

Capybara provides Element#hover method from version 2.1:

find('.some_class').hover

此方法在 Capybara :: Selenium :: Driver 中实现,其方式与@AlexD的答案相同。

This method is implemented in Capybara::Selenium::Driver in almost the same way as in @AlexD's answer.

请注意在Selenium中使用 #hover

Note that to use #hover in Selenium it's usually better to turn native events on:

Capybara.register_driver :selenium do |app|
  profile = Selenium::WebDriver::Firefox::Profile.new
  profile.native_events = true
  Capybara::Selenium::Driver.new(app, :browser => :firefox, profile: profile)
end

这篇关于如何用水豚模拟鼠标悬停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆