Forward driver method – Selenium Python


This technique is used to navigate forward in a web browser's history and allows Selenium to move forward in the browser's history page executing any new navigation commands.

This Forward Driver Method in Selenium Python can improve the efficiency and accuracy of your automated testing scripts. which allows you to quickly move between.

Setup

Firefox Executable

  • Download the Firefox browser installer from here

  • Once downloaded, install the browser and an exe file will be placed automatically in C:\Program Files\Mozilla Firefox\firefox.exe. We will be needing it later.

Gecko Driver

  • Windows Users can download the gecko driver from here. For other versions see releases.

  • Extract the zip and place the “geckodriver.exe” file in C:\ directory. We will be referencing it later in our code.

Selenium Python Package

We are going to be working with the latest version of Selenium Webdriver so pip install the following −

pip3 install -U selenium
pip3 install -U webdriver-manager

Algorithm

  • Import necessary modules from Selenium

  • Set Firefox binary location using Options() function

  • Set Firefox driver path using executable_path parameter in Firefox() function

  • Launch the Firefox driver using the get() function and open the first website

  • Use the forward() method to navigate to the second website and print the title of the page

Example

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
options = Options()

# you need to download and install firefox and it will be in the following path
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'

# you need to download and install geckodriver.exe and put it in the same folder as this script
driver = webdriver.Firefox(executable_path=r'C:\geckodriver.exe', options=options)

# launch driver using the selenium webdriver and open first website
driver.get('https://tutorialspoint.com/selenium/selenium_automation_practice.htm')
print(f"Loaded first page. Title of the page is : {driver.title}")

# instruct driver using the selenium webdriver to open the second website
driver.get('https://www.tutorialspoint.com/python3/index.htm')
# step one step forward browser history
driver.forward()
print(f"Loaded second page. Title of the page is : {driver.title}")

Output

Progress is visible in the console

Loaded first page. Title of the page is : Selenium - Automation Practice
Form
Loaded second page. Title of the page is : Python 3 Tutorial

1. The first page is loaded

2. The second page is loaded

  • Import the required modules from Selenium and then set up the options for the Firefox browser.

  • The binary location is set using the path to the Firefox executable. The driver is set up using the path to the GeckoDriver executable file, which is required for Selenium to interact with the Firefox browser

  • The driver is launched using the get() function to open the first website and the title of the page is printed to the console.

  • The driver is then instructed to navigate to the second website using the get() function.

  • The forward() method steps over to the next page

Conclusion

The Selenium Python Forward Driver Method is a potent approach that may significantly improve the effectiveness and precision of your automated testing scripts. You may easily go ahead in the browser's history by utilizing the "forward()" method offered by the Selenium WebDriver API without having to issue fresh navigation commands. This can speed up your testing scripts' overall performance and save you time. The advantages of utilizing the Forward Driver Method in Selenium Python have been discussed in this blog, along with detailed instructions on how to include it into your testing scripts.

Updated on: 18-Apr-2023

435 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements