Pressing Browser's Forward Button - Selenium


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 / 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('/')
# Optional
time.sleep(5)

# Navigate to the url
driver.get('/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 /.

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.