Page Navigation with Python Selenium

In this Python Selenium article we want to learn about Page Navigation with Python Selenium, so Selenium is an open source framework that enables developers to automate web browsers. It provides different set of tools and libraries for controlling web browsers programmatically, and it is an excellent choice for web automation tasks. Selenium supports different programming languages like Python, Java, C++ and other, in here we are going to target Python programming language, also Page Navigation is one of the important tasks in web automation and scraping.

 

To work the examples of this tutorial you need some requirements, first you should have installed Python in your system, after that we need to install Python Selenium and you can use pip for that like this.

 

 

 

This is the complete code for this article, basically, this code will open Chrom Browser, after that it goes to google.com and in the search box it writes Python Selenium, after searching it will clicks on the first result of the search. this code demonstrates different web automation actions, such as navigating to a webpage, interacting with elements, waiting for specific conditions, extracting page information, navigating back, refreshing and closing the browser.

 

 

Now let’s break down above code, first we have imported required classes from Python Selenium.

 

 

This line of code initializes the Chrome WebDriver, creating an instance of the Chrome browser for automation.

 

 

In here the get() method is used to open the specified URL (‘https://www.google.com’ in this case) in the Chrome browser.

 

 

Using WebDriverWait in conjunction with the presence_of_element_located() expected condition, we wait for up to 10 seconds for the search box element to be located on the page. once found, we use send_keys() to enter the search query Python Selenium into the search box.

 

 

The submit() method is called on the search box element to submit the search query.

 

 

Using WebDriverWait and the presence_of_element_located() expected condition, we wait for up to 10 seconds for the search results element with the ID search to be located on the page.

 

 

Using the find_element() method with By.CSS_SELECTOR, we locate the first search result link on the page. after that we use the click() method to click on the link.

 

 

Using WebDriverWait and the title_contains() expected condition, we wait for up to 10 seconds for the page title to contain the text Python – Selenium.

 

 

We print the current page title using the title attribute of the driver object.

 

 

The back() method is called to navigate back to the previous page in this case, the search results page.

 

 

We use WebDriverWait and the presence_of_element_located() expected condition to wait for up to 10 seconds for the search results element to be located again after navigating back.

 

 

The refresh() method is called to refresh the search results page.

 

 

 

Learn More on Python Selenium

Leave a Comment