Navigation in Selenium

In previous section, we have discussed about the handling file upload in Selenium Python. Now we will discuss about how to automate Navigation in Selenium Python.

Objectives:

  • What is Navigation
  • Types of Navigation
  • How to handle each type of navigation
  • Practical Example

What is Navigation?

Navigation usually deals with back and forward navigation in web page.

While working with website, back and forward navigation is used often. So this should be automated so that we can navigate to corresponding page by clicking back and forward without giving any link in get().

To learn Navigation, we will use Facebook link.

While working in browser you can see there is 3 link for navigation.

  • Back Navigation
  • Forward Navigation
  • Refresh

forward():

This method will click the forward button in the existing browser. So it will take you to the next page based on the browser history.

driver. forward();  

back():

This method will click the back button of the current browsers. So this will take you to the previous page based on the browser history.

driver. back();  

refresh():

This method will refresh your current browser. So this will  not accept any parameter or not return nay value.

driver. refresh();  

So, we can automate all these 3 Navigation operation in selenium.

Example:

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chromeimport ChromeDriverManager
import time

driver = webdriver.Chrome(ChromeDriverManager().install())

driver.implicitly_wait(10)
driver.get('https://www.facebook.com/')
driver.maximize_window()

forgot_link = driver.find_element(By.CLASS_NAME, '_6ltj')
forgot_link.click()
driver.back()
time.sleep(10)
driver.forward()
time.sleep(15)
driver.refresh()
driver.sleep(10)
driver.quit()

Explanation:

In the above example you can see we have 3 methods for back, forward and refresh respectively. driver.back() is for Backward navigation, driver.forward() is for Forward navigation, driver.refresh() is for Refresh the Page. As this process are very fast, we have introduced sleep()  for observation.

Summary:

  • Navigation usually deals with back and forward navigation in web page.
  • The method forward()will click the forward button in the existing browser. So it will take you to the next page based on the browser history.
  • The method back() will click the back button of the current browsers. So this will take you to the previous page based on the browser history.
  • The method refresh() will refresh your current browser. So this will  not accept any parameter or not return nay value.

Leave a Reply

Your email address will not be published. Required fields are marked *