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()
driver.get("http://the-internet.herokuapp.com/hovers")
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
ActionChains(driver).move_to_element(image).click(link).perform()

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.

Magic.

 

Install Selenium for fun (and profit!)

So you want to install Selenium?

 

There are a couple ways to install Selenium on your system. If you already have PIP installed, it’s as easy as running the following from your command line:

pip install -U selenium

This will check to see if you already have selenium installed and, if you do, upgrade it if there is one available. If you don’t, it’ll install the python bindings for Selenium.

Python bindings?! Yep, Selenium comes in several flavors. Head over to the download page for Selenium to check them out and find the programming language you’re most familiar with.

Another way to install Selenium is to download the source distribution from PyPi, unarchive it, and install it using the following Python command:

python setup.py install

This will run the setup.py via the Python version that’s installed on your system.

 

The commands above should be system-independent meaning you can run them on Windows, Linux, Mac, etc.selenium logo

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…)