Backward and Forward Navigation



We can move backward and forward in browser history with the help of the Selenium webdriver with Python. To navigate a step forward in history the method forward is used. To navigate a step backward in history the method back is used.

The syntax for backward and forward navigation is as follows −

driver.forward() 
driver.back()

Code Implementation

The code implementation for backward and forward navigation is as follows −

from selenium import webdriver
driver = webdriver.Chrome(executable_path='../drivers/chromedriver')
#implicit wait time
driver.implicitly_wait(0.8)
#url 1 launch
driver.get("https://www.tutorialspoint.com/about/about_careers.htm")
#url 2 launch
driver.get("https://www.tutorialspoint.com/online_dev_tools.htm")
#back in history
driver.back()
print('Page navigated after back: ' + driver.title)
#forward in history
driver.forward()
print('Page navigated after forward: ' + driver.title)
#driver quit
driver.quit()

Output

Backward and Forward Navigation

The output shows the message - Process with exit code 0 meaning that the above Python code executed successfully. After launching the two URLs, the webdriver navigates back in the browser history and the title of the previous page(obtained from the driver.title method) - About Careers at Tutorialspoint - Tutorialspoint gets printed in the console.

Again, the webdriver navigates forward in the browser history and the title of the following page(obtained from the driver.title method) - Online Development and Testing Tools gets printed in the console.

Advertisements