Selenium Python for testers
Mastering Selenium Python: Handling Hover Actions
Selenium stands out as a powerful tool for automating web browsers, enabling developers and testers to simulate user interactions with web pages. This guide introduces you to handling hover actions over a button using Selenium with Python, a common requirement in modern web applications. We’ll use the Practice Automated Testing website as a learning platform, focusing specifically on hover actions.
Getting Started with Selenium and Python
Before diving into the specifics of hover actions, ensure you have Python and Selenium installed. Selenium WebDriver acts as the bridge between your Python scripts and web browser actions. You can install Selenium in your Python environment using pip:
pip install selenium
Additionally, you’ll need a WebDriver for the browser you intend to automate. In this example, we’re using ChromeDriver, but similar drivers are available for other browsers as well.
Setting Up WebDriver
The first step is to set up WebDriver. This involves initializing the driver and navigating to the target webpage. Here’s how to do it for Chrome:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
# Setup WebDriver
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
# Navigate to the webpage
driver.get("https://www.practiceautomatedtesting.com/webelements/HoverToClickButton")
print("Opened practice automated testing")
This code automatically downloads the latest version of ChromeDriver and opens the specified URL.
Working with Hover Actions
Hover actions are common in web applications, often used to reveal hidden elements or menus. Selenium handles such interactions with the ActionChains class, which lets you queue up a series of actions to perform.
Here’s how you can perform a hover action in Selenium with Python:
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
# Wait for the hoverable element to be present
hover_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '[class="HoverToClickButton_hoverText__wtIgh"]'))
)
# Perform the hover action
actions = ActionChains(driver)
actions.move_to_element(hover_element).perform()
time.sleep(4) # Pause to see the effect
# Wait for the hidden button to become visible
hidden_button = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, '[class="HoverToClickButton_hiddenButton__WL3-l"'))
)
# Check if the hidden button is now visible
if hidden_button.is_displayed():
print("OK: Hidden button is now visible.")
This example demonstrates waiting for a specific element (which becomes visible only when hovered over) to be present. Then, it uses ActionChains to hover over the element and waits for a hidden button to become visible as a result of the hover action.
Conclusion
Handling hover actions in Selenium with Python is straightforward once you get the hang of the ActionChains class. This tool is essential for testing web applications that react to mouse movements. By mastering hover actions, you can ensure your automated tests more accurately reflect user interactions, leading to more reliable and comprehensive testing outcomes.
Remember, practice is key to mastering Selenium. The Practice Automated Testing website provides an excellent sandbox for honing your skills in a controlled environment.
Closing the Driver
Don’t forget to close the WebDriver session once your automation script is complete to free up system resources:
driver.quit()
Whether you’re automating form submissions, practicing different user interactions, or ensuring your web applications are as robust as they can be, Selenium with Python is an invaluable ally in your development toolkit.