How to hover over (mouseover) an element in Selenium Ruby?

12,100

Solution 1

Turns out the answer is:

driver.move_to(el).perform

I forgot the .perform.

Solution 2

I used driver.action.move_to(el).perform which differs ever so slightly from the other answers, so I thought I would include it for completeness sake.

Solution 3

This works for me:

driver.mouse.move_to el

Solution 4

You need to use Selenium's Action Builder to access more complex actions like hovering (which is what seanny123's answer is demonstrating).

Also, if you are working with a hover, odds are you will need to dynamically wait for it to display before taking your next action (e.g., using an explicit wait).

I put together an example on how to do this -- you can see the full write-up here.

Solution 5

To hover an element:

driver.action.move_to(element).perform
# e.g.    
driver.action.move_to(driver.find_element(css: 'a')).perform

To hover an element at a specific location:

driver.action.move_to(element, mouse_x, mouse_y).perform
# e.g.    
driver.action.move_to(driver.find_element(css: 'a'), 100, 100).perform
Share:
12,100
kidcapital
Author by

kidcapital

twitter.com/kidcapital

Updated on June 23, 2022

Comments

  • kidcapital
    kidcapital almost 2 years

    Anyone know how to hover over an element in Selenium Ruby Webdriver?

    My code is like this:

    el = driver.find_element(:css => "#foo")
    driver.move_to el # How do I trigger a mouseover event on this element?
    

    I'm using selenium-webdriver gem with Firefox in Linux 32-bit.

  • Dan Sabin
    Dan Sabin about 10 years
    currently selenium has forked off the move_to function so you need to use .action to access the mouse functions now.
  • Dan Sabin
    Dan Sabin about 10 years
    Check @Seanny123 and my comment below on the answer about why this may now work.
  • Dan Sabin
    Dan Sabin about 10 years
    Check @Seanny123 and my comment there about why this may now work.
  • Seanny123
    Seanny123 over 7 years
    @DanSabin would you like to propose an edit clarifying this? I would totally accept it.