Pressing Browser’s Back Button – Selenium

Contents

Pressing the Browser’s Back Button

To simulate pressing of the browser’s back button programmatically using Selenium for Python, call back() method on the Web Driver object.

driver.back()

Example

  1. Initialise Chrome Web Driver object as driver.
  2. Get the URL https://pythonexamples.org/ using web driver object.
  3. Find element by link text 'OpenCV' and click on the element.
  4. Make an implicit wait of 5 seconds.
  5. Press browser’s back button using driver.back().

Python Program

from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By

service = Service(executable_path="/usr/local/bin/chromedriver")

# Initialize web driver
with webdriver.Chrome(service=service) as driver:
    #navigate to the url
    driver.get('https://pythonexamples.org/')
    #find element that has link text : 'OpenCV'
    driver.find_element(By.LINK_TEXT, 'OpenCV').click()
    #press on browser's back button
    driver.back()

Screenshots

Navigation Step #1 : Get the URL https://pythonexamples.org/.

Pressing Browser's Back Button - Selenium

Navigation Step #2 : Find OpenCV link and click on it.

Pressing Browser's Back Button - Selenium

Navigation Step #3 : Press the browser’s back button.

Pressing Browser's Back Button - Selenium

Summary

In this Python Selenium Tutorial, we learned how to press the browser’s back button programmatically using driver.back() method.

Related Tutorials

Quiz on Selenium

Q1. What is the method used to find web elements in Selenium Python?

Not answered

Q2. Which of the following is not a method to wait for a web element to load in Selenium Python?

Not answered

Q3. What is the method used to navigate back to the previous page in Selenium Python?

Not answered

Q4. What is the method used to switch to a different frame in Selenium Python?

Not answered

Q5. What is the method used to clear the text from an input field in Selenium Python?

Not answered
Code copied to clipboard successfully 👍