About this lesson
Selenium Python hover with ActionChains move_to_element: read a color before and after
Selenium Python AssignmentsAdvancedMouse and Keyboard
How it works
An element has no hover() method in Selenium. Mouse movement lives in ActionChains, imported from selenium.webdriver.common.action_chains. You build a chain with ActionChains(driver).move_to_element(element) and finish with .perform(). Without perform() nothing happens: the chain only records your wish. On this page, reading the color after building the chain but before running it still gave the original value.
The button changes color through a plain CSS hover rule, with no JavaScript to wait for. So value_of_css_property("background-color") reads the computed style at the moment you ask. It returns a string shaped like rgba(r, g, b, a), not a hex code. Compare whole strings rather than splitting them.
Two Python facts help. Use the CSS spelling with a hyphen, as the camelCase form working is a Chrome detail. A made-up name gives an empty string, not an exception. And perform() returns None, so read the color from the element again after the hover.
About the assignment
Capture one button's background twice, at rest and with the pointer over it, and print both next to the label text of the button.
What you will do
- Find the hover button by its id and read its background color into a variable before any mouse action.
- Run a chain that moves the pointer to that button.
- Read the background color a second time, then read the button text.
- Print three labelled lines: text, before, after.
Common mistakes
Forgetting perform()
The chain object is created but never runs. The second color equals the first, and nothing warns you.
Reading both colors at the same moment
If both variables come from reads taken before the hover, they match. Take the second reading after the pointer moved.
Misspelling the property
Asking for
background-colourreturns an empty string. No error appears, so a typo shows up only in your output.
In a real test suite
Page objects often expose a method such as hover_menu() that wraps the chain, so tests read as plain actions. Hover is also how many dropdown menus open before a click.
The next lessons keep using ActionChains for other mouse gestures, starting with a double click.
Related lessons
- Read a CSS PropertyReads a CSS property from an element without any mouse action.
- Double-Click a ButtonAnother
ActionChainsgesture on the same page. - Right-Click a ButtonRight click uses the same build then
perform()pattern.
To try one of these calls on any page without a task, open the Online Selenium Python Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
try:
driver.get("https://testkru.com/Elements/Buttons")
# Print the text and the color before and after a hover
finally:
driver.quit()