Navigating between pages is a fundamental capability of Selenium. This module will cover how to interact with web elements like buttons, text inputs, and dropdowns to navigate within a page or move to other pages. It also includes handling links, navigating to URLs, and controlling browser history.
When encountering various web pages, you will notice many interactive-based elements. Fortunately, when using Selenium navigation, you can use the following approaches to interact with them.
Buttons are commonly used for navigation, such as submitting forms or triggering AJAX requests.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
# Locate and click a button
button = driver.find_element("id", "nextPageButton")
button.click()
Sending text to input fields is essential for filling out forms and triggering search results.
# Locate the input field and send text
search_box = driver.find_element("name", "q")
search_box.send_keys("Selenium tutorial")
search_box.submit() # Optionally, submit the form
Dropdowns allow users to select from a predefined set of options.
from selenium.webdriver.support.ui import Select
# Locate the dropdown element
dropdown = Select(driver.find_element("id", "dropdownId"))
# Select an option by visible text
dropdown.select_by_visible_text("Option 1")
# Select an option by index
dropdown.select_by_index(2)
# Select an option by value
dropdown.select_by_value("value1")
Links are one of the most common ways to navigate between pages. Selenium supports interacting with links through their text or attributes.
Links appear on virtually every web page, so your navigation efforts in Selenium will often need to interact with them.
# Locate and click a link by its text
link = driver.find_element("link text", "Go to Page")
link.click()
For links with dynamic text, use partial text matching or XPath.
link = driver.find_element("partial link text", "Go")
link.click()
Sometimes, it's more efficient to navigate directly to a URL.
driver.get("https://example.com/page2")
You can control browser history to revisit pages.
# Go to a URL
driver.get("https://example.com")
# Navigate to another page
driver.get("https://example.com/page2")
# Go back to the previous page
driver.back()
# Go forward to the next page
driver.forward()
Some navigation triggers involve JavaScript, like modals or AJAX-based buttons. Selenium can handle these interactions seamlessly.
# Clicking a JavaScript-triggered button
button = driver.find_element("id", "jsButton")
button.click()
# Wait for the page to load (if necessary)
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
wait.until(EC.url_changes("https://example.com"))
A typical web scraping or automation scenario often involves combining multiple navigation methods.
Example Workflow:
driver.get("https://example.com/login")
# Login
username = driver.find_element("id", "username")
password = driver.find_element("id", "password")
username.send_keys("test_user")
password.send_keys("secure_password")
driver.find_element("id", "loginButton").click()
# Navigate to results
search_box = driver.find_element("id", "search")
search_box.send_keys("Selenium tutorial")
search_box.submit()
# Click on a result link
link = driver.find_element("partial link text", "Selenium Basics")
link.click()
# Go back to the results page
driver.back()
This module equips you with strategies to navigate complex workflows in Selenium effectively, laying the foundation for robust automation scripts.
Our community is here to support your growth, so why wait? Join now and let’s build together!