Hover event in Selenium

Hover using Selenium webdriver’s ActionChains

I was recently trying to figure out how to perform a hover event using Selenium’s webdriver. The team I had previously worked on wrote a nifty function using execute_script invoking jQuery. It looked something like this:

self.driver.execute_script("jQuery('%s').mouseover()" % (hover_selector))

This didn’t always work the way I expected so I did some research and came across webdriver’s ActionChains. From Selenium-Python’s ReadTheDocs:

ActionChains are a way to automate low level interactions such as mouse movements, mouse button actions, key press, and context menu interactions. This is useful for doing more complex actions like hover over and drag and drop.

I whipped open my terminal, fired up webdriver and headed over to the trusty the-internet to give it a whirl.

The page I ran this test on was the-internet.herokuapp.com/hovers.

Here’s the code:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
image = driver.find_element_by_css_selector("div.figure:nth-child(3)")
link = driver.find_element_by_css_selector("div.figure:nth-child(3) a")
# Now comes the magic

When I ran this in my console, the browser hovered over the image then clicked on the View Profile link that appears after the hover.



jenkins logo

What is Jenkins doing?

I wrote some automation tests using Selenium as I build a framework for the project I’m currently working on. Since I’m probably going to go with Jenkins to run them, I got him setup on my Windows 7 machine as I run through the tests. I ran into one problem: The tests would run fine if I ran them via the command line but they were failing when Jenkins ran them. I couldn’t figure out why.

What is that Jenkins guy doing?

After some investigating, I accidentally discovered a nifty setting in Windows (more…)