Pressing Browser’s Forward Button – Selenium

Contents

Pressing the Browser’s Forward Button

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

driver.forward()

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. Press browser’s back button using driver.back().
  5. Press browser’s forward button using driver.forward().

Python Program

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
import time

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.set_window_size(600,800)

# Navigate to the url
driver.get('https://pythonexamples.org/')

# Optional
time.sleep(5)

# Navigate to the url
driver.get('https://pythonexamples.org/selenium/')

# Optional
time.sleep(5)

# Simulate browser's back button
driver.back()

# Optional
time.sleep(5)

# Simulate browser's forward button
driver.forward()

# Optional
time.sleep(5)

# Close the driver
driver.quit()

Screenshots

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

Open URL in browser

Navigation Step #2 : Navigate to Selenium tutorials page.

Open another URL in browser

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

Press browsers back button using selenium

Navigation Step #4 : Press the browser’s forward button.

Pressing the Browser's Forward Button using Selenium

Summary

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

Related Tutorials

Quiz on Selenium

Q1. What is a web driver in Selenium Python?

Not answered

Q2. Which of the following is not a popular web driver used in Selenium Python?

Not answered

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

Not answered

Q4. What is the method used to take a screenshot in Selenium Python?

Not answered

Q5. What is the method used to select a value from a drop-down list in Selenium Python?

Not answered
Code copied to clipboard successfully 👍