selenium
  1. selenium-navigation-commands

Navigation Commands

The Selenium WebDriver provides various navigation commands that are used to navigate through web pages. The following are the navigation commands supported by Selenium WebDriver:

Syntax

webdriver.ActionChains(driver).<command_name>().perform()

Example

from selenium import webdriver

driver = webdriver.Chrome()

# Navigate to a URL
driver.get("https://www.google.com")

# Click on an element
elem = driver.find_element_by_name("q")
webdriver.ActionChains(driver).move_to_element(elem).click().perform()

# Go back to previous page
driver.back()

# Go forward to the next page
driver.forward()

# Refresh the current page
driver.refresh()

# Quit the browser
driver.quit()

Explanation

  • get(url): it loads the given URL in the current browser session.
  • back(): it navigates the current browser session one page back.
  • forward(): it navigates the current browser session one page forward.
  • refresh(): it refreshes the current browser session.
  • quit(): it closes the browser session.

Use

Navigation commands are used to navigate to different pages, go back to previous pages, and refresh the current page in automated tests. These commands are used to simulate user interactions with the browser.

Important Points

  • Navigation commands can only be used with a valid WebDriver instance.
  • The driver.get(url) command is equivalent to typing a URL into the browser address bar and pressing enter.
  • The driver.back() command is equivalent to pressing the browser's back button.
  • The driver.forward() command is equivalent to pressing the browser's forward button.
  • The driver.refresh() command is equivalent to pressing the browser's refresh button.

Summary

In this article, we have covered the various navigation commands supported by Selenium WebDriver, including get(), back(), forward(), refresh(), and quit(). These commands are essential for automated testing and simulate real user interactions with the browser.

Published on: