All Courses
Scraping

Selenium Navigation Between Pages

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.

Interaction-Based Selenium Navigation

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.

Clicking Buttons

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

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

Selecting Dropdown Options

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")

Link-Based Navigation

Links are one of the most common ways to navigate between pages. Selenium supports interacting with links through their text or attributes.

Clicking Links

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()

Handling Dynamic or Partial Links

For links with dynamic text, use partial text matching or XPath.

link = driver.find_element("partial link text", "Go")
link.click()

Direct Navigation

Sometimes, it's more efficient to navigate directly to a URL.

Navigating to a URL

driver.get("https://example.com/page2")

Navigating Backwards and Forwards

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()

Handling JavaScript-Based Navigation

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"))

Combining Navigation Techniques

A typical web scraping or automation scenario often involves combining multiple navigation methods.

Example Workflow:

  1. Open a webpage.
  2. Fill out a form.
  3. Submit the form to navigate to a results page.
  4. Click a link to view detailed information.
  5. Navigate back to the results page.
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()

Best Practices for Navigation

  • Prioritize Efficiency: Use direct URL navigation when possible.
  • Use Explicit Waits: Ensure dynamic elements are interactable.
  • Test Locators: Validate element locators using browser developer tools.

This module equips you with strategies to navigate complex workflows in Selenium effectively, laying the foundation for robust automation scripts.

Join Our Community!

Our community is here to support your growth, so why wait? Join now and let’s build together!

ArrowArrow
Try Rayobyte proxies for all your scraping needs
Explore Now

See What Makes Rayobyte Special For Yourself!