Handling multiple pages in Selenium Python - Build an Automation Script
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 def test_multiple_page_navigation(): driver = webdriver.Chrome() wait = WebDriverWait(driver, 10) try: # Step 1: Open home page driver.get('https://example.com/home') # Step 2: Click 'About Us' link about_link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, 'About Us'))) about_link.click() # Step 3: Verify URL is about page wait.until(EC.url_to_be('https://example.com/about')) assert driver.current_url == 'https://example.com/about', f"Expected URL to be 'https://example.com/about' but got {driver.current_url}" # Step 4: Verify heading on About page about_heading = wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'h1'))) assert about_heading.text == 'About Our Company', f"Expected heading 'About Our Company' but got '{about_heading.text}'" # Step 5: Click 'Contact' link contact_link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, 'Contact'))) contact_link.click() # Step 6: Verify URL is contact page wait.until(EC.url_to_be('https://example.com/contact')) assert driver.current_url == 'https://example.com/contact', f"Expected URL to be 'https://example.com/contact' but got {driver.current_url}" # Step 7: Verify heading on Contact page contact_heading = wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'h1'))) assert contact_heading.text == 'Contact Information', f"Expected heading 'Contact Information' but got '{contact_heading.text}'" finally: driver.quit()
This test script uses Selenium WebDriver with Python to automate the manual test case.
We start by opening the browser and navigating to the home page URL.
We use explicit waits to wait until the 'About Us' link is clickable, then click it.
We wait until the URL changes to the About page URL and assert it matches exactly.
We then wait for the heading element (assumed to be an <h1>) to be visible and check its text.
Next, we click the 'Contact' link similarly, wait for the URL to update, and verify the heading on the Contact page.
Finally, we close the browser in a finally block to ensure cleanup even if assertions fail.
This approach uses best practices like explicit waits and meaningful locators to make the test reliable and readable.
Now add data-driven testing with 3 different navigation paths and verify their URLs and headings.
Practice
driver.window_handles return in Selenium?Solution
Step 1: Understand what window handles represent
Window handles are unique IDs for each open browser window or tab.Step 2: Identify what
This property returns a list of all window handles currently opened by the driver.driver.window_handlesreturnsFinal Answer:
A list of all open browser window handles -> Option DQuick Check:
window_handles = list of all windows [OK]
- Confusing window_handles with current window handle
- Expecting window_handles to return page titles or URLs
- Using window_handles as a single string instead of a list
Solution
Step 1: Recall the correct method to switch windows
The Selenium method to switch windows isdriver.switch_to.window().Step 2: Check the syntax for switching to the second tab
Use the second handle in the list:driver.window_handles[1].Final Answer:
driver.switch_to.window(driver.window_handles[1]) -> Option CQuick Check:
Use switch_to.window with window_handles index [OK]
- Using non-existent methods like switch_window or switch_to_tab
- Forgetting to use driver.switch_to
- Using wrong index for window_handles
main_window = driver.current_window_handle
all_windows = driver.window_handles
for window in all_windows:
driver.switch_to.window(window)
print(driver.title)
Solution
Step 1: Understand the loop over all window handles
The code loops over all open windows and switches to each one in order.Step 2: Print the title of each window after switching
It prints the title of each tab in the order of window_handles list, which is usually the order tabs opened.Final Answer:
["Home", "About"] -> Option AQuick Check:
Loop prints titles of all tabs in order [OK]
- Assuming only current tab title prints
- Mixing order of window handles
- Not switching before printing title
main_window = driver.current_window_handle # code to open new tab driver.switch_to.window(driver.window_handles[1]) # do something # switch back driver.switch_to.window(main_window())
Solution
Step 1: Check how main_window is used
main_window is a string handle, but code calls main_window() as a function, which is incorrect.Step 2: Identify correct usage to switch back
Should usedriver.switch_to.window(main_window)without parentheses.Final Answer:
main_window is called like a function with parentheses -> Option BQuick Check:
Window handle is a string, not a function [OK]
- Calling window handle variables as functions
- Using wrong index for window_handles
- Assuming switch_to needs import
Solution
Step 1: Save the original window handle before opening new tabs
This allows you to remember which tab was the main one.Step 2: After closing new tabs, switch back to the saved original handle
Usedriver.switch_to.window(original_handle)to return safely.Final Answer:
Save the original window handle before opening new tabs and switch back using it -> Option AQuick Check:
Save and reuse original handle to return [OK]
- Assuming last window handle is original
- Closing tabs without switching back
- Using driver.quit() which ends session
Ready to prove it?
Pass the test and this pattern moves unknown
