Challenge - 5 Problems
Mouse Hover Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Selenium Python code snippet?
Consider the following Selenium Python code that performs a mouse hover action. What will be printed after execution?
Selenium Python
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.by import By class DummyElement: def __init__(self): self.hovered = False def get_attribute(self, attr): if attr == 'class': return 'hovered' if self.hovered else 'not-hovered' # Simulate hover effect by setting hovered to True def hover_element(element): element.hovered = True # Test code element = DummyElement() actions = ActionChains(None) # driver is None for simulation hover_element(element) print(element.get_attribute('class'))
Attempts:
2 left
💡 Hint
Think about what happens to the element's state after the hover_element function is called.
✗ Incorrect
The hover_element function sets the element's hovered attribute to True. The get_attribute method returns 'hovered' if hovered is True, so the output is 'hovered'.
❓ assertion
intermediate2:00remaining
Which assertion correctly verifies the hover effect on a web element?
You want to check if a web element has the CSS class 'active' after a mouse hover. Which assertion is correct?
Selenium Python
element = driver.find_element(By.ID, 'menu-item') actions = ActionChains(driver) actions.move_to_element(element).perform() # Assertion options:
Attempts:
2 left
💡 Hint
Check the element's class attribute after hover.
✗ Incorrect
The CSS class 'active' is part of the class attribute string. Checking if 'active' is in element.get_attribute('class') correctly verifies the hover effect.
❓ locator
advanced2:00remaining
Which locator is best to find a menu item that appears only after mouse hover?
A submenu item appears only after hovering over a main menu. Which locator strategy is best to find this submenu item reliably?
Attempts:
2 left
💡 Hint
Consider that the submenu is only visible after hover and may have a style change.
✗ Incorrect
Using XPath to check the submenu's style attribute for 'display: block' ensures the submenu is visible before locating the item, making it reliable after hover.
🔧 Debug
advanced2:00remaining
Why does this mouse hover test fail to reveal the submenu?
This Selenium Python code tries to hover over a menu but the submenu does not appear. What is the likely cause?
Selenium Python
menu = driver.find_element(By.ID, 'menu') actions = ActionChains(driver) actions.move_to_element(menu) # Missing perform() submenu = driver.find_element(By.ID, 'submenu') assert submenu.is_displayed()
Attempts:
2 left
💡 Hint
Check if the action chain is actually executed.
✗ Incorrect
Without calling perform(), the move_to_element action is not sent to the browser, so the hover does not happen and submenu remains hidden.
❓ framework
expert3:00remaining
How to implement a reliable mouse hover test with explicit wait in Selenium Python?
You want to hover over a menu and wait until the submenu is visible before asserting. Which code snippet correctly implements this?
Selenium Python
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.action_chains import ActionChains menu = driver.find_element(By.ID, 'menu') actions = ActionChains(driver) # Choose the correct code to hover and wait for submenu:
Attempts:
2 left
💡 Hint
Remember to perform the hover and use explicit wait for visibility.
✗ Incorrect
Option B correctly performs the hover action, then waits explicitly for the submenu to be visible before asserting. Option B misses perform(), C uses sleep instead of explicit wait, and A clicks instead of hover.