In this article we want to learn about Page Navigation with Python Selenium, Navigating between web pages is an important part of web automation tasks using Python Selenium. If you are scraping data, testing web applications, or interacting with dynamic content, then it is important to know about navigating efficiently. In this lesson we want to learn different techniques for page navigation with Python Selenium.
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.
1 |
pip install selenium |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # First Initialize Chrome WebDriver driver = webdriver.Chrome() # Load Google homepage driver.get('https://www.google.com') # Find search box element by name and enter search query search_box = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.NAME, 'q')) ) search_box.send_keys('Python Selenium') # Submit the search query search_box.submit() # Wait up to 10 seconds for the search results to appear search_results = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'search')) ) # Click on the first search result first_result = driver.find_element(By.CSS_SELECTOR, 'div.g a') first_result.click() # Wait for the page to load WebDriverWait(driver, 10).until( EC.title_contains('Python - Selenium') ) # Print the current page title print("Current Page Title:", driver.title) # Navigate back to the search results page driver.back() # Wait for the search results page to load again WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'search')) ) # Refresh the search results page driver.refresh() # Close the WebDriver instance driver.quit() |
Now let’s talk above code, first we have imported required classes from Python Selenium.
1 2 3 4 |
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC |
This line of code initializes the Chrome WebDriver, creating an instance of the Chrome browser for automation.
1 |
driver = webdriver.Chrome() |
In here the get() method is used to open the specified URL (‘https://www.google.com’ in this case) in the Chrome browser.
1 |
driver.get('https://www.google.com') |
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.
1 2 3 4 |
search_box = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.NAME, 'q')) ) search_box.send_keys('Python Selenium') |
The submit() method is called on the search box element to submit the search query.
1 |
search_box.submit() |
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.
1 2 3 |
search_results = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'search')) ) |
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.
1 2 |
first_result = driver.find_element(By.CSS_SELECTOR, 'div.g a') first_result.click() |
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.
1 2 3 |
WebDriverWait(driver, 10).until( EC.title_contains('Python - Selenium') ) |
We print the current page title using the title attribute of the driver object.
1 |
print("Current Page Title:", driver.title) |
The back() method is called to navigate back to the previous page in this case, the search results page.
1 |
driver.back() |
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.
1 2 3 |
WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'search')) ) |
The refresh() method is called to refresh the search results page.
1 |
driver.refresh() |
FAQs about Page Navigation with Python Selenium:
Q: How do I open a URL in Python Selenium?
A: You can open a URL in Python Selenium using get() method of the WebDriver instance. For example: driver.get(“https://www.example.com”).
Q: Can I refresh the current web page using Python Selenium?
A: Yes, you can refresh the current page using the refresh() method: driver.refresh().
Q: How do I navigate forward and backward in the browser history with Python Selenium?
A: You can navigate forward and backward using forward() and back() methods, respectively: driver.forward() and driver.back().
Q: Are there any limitations to page navigation in Python Selenium?
A: While Python Selenium offers a lot of capabilities for page navigation, there may be limitations in handling complex scenarios or dynamic content. It’s essential to carefully test your automation scripts and adapt your approach as needed to handle specific challenges.
Learn More on Python Selenium
- Python Selenium Web Scraping
- Test Automation with Python Selenium
- Python Selenium WebDriver Tutorial
- Browser Interaction in Python Selenium
- Browser Profiles in Python Selenium
- Python Selenium Frames
Subscribe and Get Free Video Courses & Articles in your Email