Using Capybara and Selenium to hover over an element

I have a link on a page that appears only when hovering over a specific element. But I can’t figure out how to emulate this, and then click on the link so that I can check it with Cucumbers using Capybara and Selenium.

Does anyone know how to do this? I tried to execute javascript and also tried to talk directly to the selenium driver, but so far I have not been very lucky ...

+8
selenium cucumber capybara
source share
4 answers

This question has been around for several years, so the answer may have changed. There are several different ways to do this in newer versions of Capybara, depending on the driver used.

For selenium, you can do javascript:

page.execute_script '$("#element").trigger("mouseover")' 

Newer versions of selenium-webdriver support a hover method:

 find('#element').hover 

For capybara-webkit, the driver supports triggering events on an element:

 find('#element').trigger(:mouseover) 

I originally found the answer to this question from here and here .

+3
source share

Use Selenium fireEvent in the mouseover event, and then pause the required hover time.

+2
source share

I worked on a similar one that I used to see that this could help you.

Using the Selenium IDE.
1. Go to the desired page.
2. Run the selenium IDE and get the link.
3. Change the command to mouseOver . Double-click on a command in the Selenium IDE.
4. It will show the link you needed.
5. Start recording again and record the required link.

When you double-click on the mouseOver command, it creates a mouse simulation through the IDE, and you can use your mouse to record a pop-up link.

thanks

0
source share

I finally managed to make the Capybara + Selenium driver “hang over the element”. This is the code I'm using:

 module Capybara module Node class Element def hover @session.driver.browser.action.move_to(self.native).perform end end end end 
0
source share

All Articles