Automating browser interactions using Python with Selenium is a powerful way to handle testing of web applications, perform repetitive tasks, or scrape data from the web. In this lessen, I’ll walk you through the basics of setting up Selenium with Python(But this counts for all programming languages) and how to perform a variety of common web interactions.
Prerequisites
Before starting, you need Python installed on your system. You also need to install Selenium and webdriver_manager, which helps in managing the browser drivers easily.
Opening a Web Page
Let’s start by opening a web page. We’ll open Google for this example.
driver.get("https://www.google.com")
print("Opened Google.")
Interacting with Web Elements
Interacting with web elements like text boxes, buttons, or links is core to using Selenium.
Finding Elements
Selenium provides several methods to find elements on a page, such as find_element_by_name, find_element_by_id, find_element_by_xpath, etc.
Example: Search on Google
# Find the search box
search_box = driver.find_element_by_name('q')
# Enter text
search_box.send_keys('Hello World')
# Submit the form
search_box.submit()
print("Performed Google Search.")
Clicking a Button
To click a button or any element, first find it, then use the click() method.
Example: Clicking a Link
# Find the link by link text and click
link = driver.find_element_by_link_text('Images')
link.click()
print("Navigated to Google Images.")
Handling Waits
Web pages may take time to load, especially after a click or form submission that requires a page refresh. Selenium handles this with two types of waits: implicit and explicit.
Implicit Wait
Tells WebDriver to poll the DOM for a certain amount of time when trying to find any element (or elements) not immediately available.
driver.implicitly_wait(10) # seconds
Explicit Wait
Waits for a certain condition to occur before proceeding.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Wait for one element to be clickable
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.NAME, 'btnK'))
)
Closing the Browser
driver.quit()
print("Closed the browser.")
Always remember to close your browser when your automation is complete.