How to navigate back in selenium python

In this post, we are going to learn how to navigate back in selenium Python. The selenium API is used to perform automated testing in Python To navigate back to the previous page selenium driver.back() method is used and We can navigate forward by using the selenium driver.forward() method is used.

How to navigate back in selenium Python


  • driver.back(): The selenium driver API driver. back() method is used to navigate to the previous page.
  • execute_script(): The selenium API javascript executor helps us to run the javascript command in the current window/frame

1. How to navigate back in selenium python by using driver.back()


In this program, we are launching the Chrome browser with the help of ChromeDriverManager and navigating to the URL by the brodriver. get(“https://accounts.google.com/”) method and navigate to Second Page “gmail.com” . We waited for 2 seconds and navigate back to the previous page with the help of the driver.back() method

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
 
 
brodriver= webdriver.Chrome(ChromeDriverManager().install())
brodriver.maximize_window()
 
brodriver.get("https://accounts.google.com/") 
 
print("The first Page Title  = " + brodriver.title)
brodriver.get("https://gmail.com/") 
print("The Second Page Title:",brodriver.title)
time.sleep(2)
brodriver.back()
print("The Page Title after go back:",brodriver.title)

Output

The first Page Title  = Sign in - Google Accounts
The Second Page Title: Gmail: Free, Private & Secure Email | Google Workspace
The Page Title after go back: Sign in - Google Accounts

2. How to navigate back in selenium python by using execute_script()


We have used brodriver.execute_script() selenium driver API method to navigate back to the previous page in python. To run a javascript window. history.go(-1) method in the current window passed as argument to selenium execute_script() method.

We have passed the service argument value using ChromeDriverManager(). The chrome driver manager automatically handles the executable path. to avoid the warning “DeprecationWarning: executable_path has been deprecated “.

  • we are launching the Chrome browser with the help of ChromeDriverManager and navigating to the URL by using brodriver. get(“https://accounts.google.com/”) method.
  • Navigate to Second Page “gmail.com”. We waited for 2 seconds.
  • Now, navigate back to the previous page with the help of the execute_script(“window.history.go(-1)”) method
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
 
options = Options()
options.add_argument("start-maximized")
brodriver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

 
brodriver.get("https://accounts.google.com/") 
 
print("The first Page Title  = " + brodriver.title)
brodriver.get("https://gmail.com/") 
print("The Second Page Title:",brodriver.title)
time.sleep(2)
brodriver.execute_script("window.history.go(-1)")
print("The Page Title after go back:",brodriver.title)

Output

The first Page Title  = Sign in - Google Accounts
The Second Page Title: Gmail: Free, Private & Secure Email | Google Workspace
The Page Title after go back: Sign in - Google Accounts