- Selenium Webdriver Tutorial
- Home
- Introduction
- Installation
- Browser Navigation
- Identify Single Element
- Identify Multiple Elements
- Explicit and Implicit Wait
- Pop-ups
- Backward and Forward Navigation
- Cookies
- Exceptions
- Action Class
- Create a Basic Test
- Forms
- Drag and Drop
- Windows
- Alerts
- Handling Links
- Handling Edit Boxes
- Color Support
- Generating HTML Test Reports in Python
- Read/Write data from Excel
- Handling Checkboxes
- Executing Tests in Multiple Browsers
- Headless Execution
- Wait Support
- Select Support
- JavaScript Executor
- Chrome WebDriver Options
- Scroll Operations
- Capture Screenshots
- Right Click
- Double Click
- Selenium Webdriver Useful Resources
- Selenium WebDriver - Quick Guide
- Selenium WebDriver - Useful Resources
- Selenium WebDriver - Discussion
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
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.