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
- Initialise Chrome Web Driver object as
driver
. - Get the URL
https://pythonexamples.org/
using webdriver
object. - Find element by link text
'OpenCV'
and click on the element. - Press browser’s back button using
driver.back()
. - 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](https://pythonexamples.org/wp-content/uploads/2023/08/screenshot-1-1.png)
Navigation Step #2 : Navigate to Selenium tutorials page.
![Open another URL in browser](https://pythonexamples.org/wp-content/uploads/2023/08/screenshot-2-1.png)
Navigation Step #3 : Press the browser’s back button.
![Press browsers back button using selenium](https://pythonexamples.org/wp-content/uploads/2023/08/screenshot-3.png)
Navigation Step #4 : Press the browser’s forward button.
![Pressing the Browser's Forward Button using Selenium](https://pythonexamples.org/wp-content/uploads/2023/08/screenshot-4.png)
Summary
In this Python Selenium Tutorial, we learned how to press the browser’s forward button using Selenium’s driver.forward()
method.