kidcapital
kidcapital

Reputation: 5174

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

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.

Upvotes: 6

Views: 12881

Answers (5)

Dorian
Dorian

Reputation: 23939

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

Upvotes: 1

kidcapital
kidcapital

Reputation: 5174

Turns out the answer is:

driver.move_to(el).perform

I forgot the .perform.

Upvotes: 4

Dave Haeffner
Dave Haeffner

Reputation: 324

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.

Upvotes: 1

Seanny123
Seanny123

Reputation: 9346

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.

Upvotes: 6

Victor Grey
Victor Grey

Reputation: 941

This works for me:

driver.mouse.move_to el

Upvotes: 3

Related Questions