Skip to content
CodeKruPro

Hover and Read the Color Change

56/80

Hover and Read the Color Change

ActionChains.move_to_element hovers. This button has no hover script. The color change is CSS. Print the button text and the background color before and after.

Task:

  1. Read the text and background color of #colorChangeOnHover
  2. Move to the button
  3. Print the background color again

Print the lines below exactly.

Expected output:

Button text: Hover on me
Before: rgba(255, 186, 0, 1)
After: rgba(8, 111, 109, 1)

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

  1. Find the hover button by its id and read its background color into a variable before any mouse action.
  2. Run a chain that moves the pointer to that button.
  3. Read the background color a second time, then read the button text.
  4. 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-colour returns 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.

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.

main.py
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()