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

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

问题描述

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

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 方法 来自 2.1 版:

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

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

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