Bird
Raised Fist0
Selenium Pythontesting~15 mins

Handling multiple pages in Selenium Python - Build an Automation Script

Choose your learning style10 modes available
Verify navigation and content on multiple pages
Preconditions (2)
Step 1: Open the browser and navigate to 'https://example.com/home'
Step 2: Click on the link with text 'About Us' to go to the About page
Step 3: Verify the URL changes to 'https://example.com/about'
Step 4: Verify the page contains the heading 'About Our Company'
Step 5: Click on the link with text 'Contact' to go to the Contact page
Step 6: Verify the URL changes to 'https://example.com/contact'
Step 7: Verify the page contains the heading 'Contact Information'
✅ Expected Result: The browser navigates correctly between pages, URLs update accordingly, and the correct headings appear on each page.
Automation Requirements - Selenium with Python
Assertions Needed:
Verify current URL after navigation
Verify presence and text of page headings
Best Practices:
Use explicit waits to wait for elements to be visible
Use By locators with meaningful selectors
Switch between pages by clicking links and verifying URL changes
Close the browser after test completion
Automated Solution
Selenium Python
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.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
Using XPath with absolute paths like /html/body/div[2]/a
Not verifying URL after navigation
Not closing the browser after test
Bonus Challenge

Now add data-driven testing with 3 different navigation paths and verify their URLs and headings.

Show Hint

Practice

(1/5)
1. What does driver.window_handles return in Selenium?
easy
A. The URL of the current page
B. The current active window handle only
C. The title of the current page
D. A list of all open browser window handles

Solution

  1. Step 1: Understand what window handles represent

    Window handles are unique IDs for each open browser window or tab.
  2. Step 2: Identify what driver.window_handles returns

    This property returns a list of all window handles currently opened by the driver.
  3. Final Answer:

    A list of all open browser window handles -> Option D
  4. Quick Check:

    window_handles = list of all windows [OK]
Hint: Remember: window_handles lists all open pages [OK]
Common Mistakes:
  • 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
2. Which is the correct syntax to switch to a new browser tab in Selenium Python?
easy
A. driver.switch_to_tab(driver.window_handles[1])
B. driver.switch_window(driver.window_handles[1])
C. driver.switch_to.window(driver.window_handles[1])
D. driver.change_window(driver.window_handles[1])

Solution

  1. Step 1: Recall the correct method to switch windows

    The Selenium method to switch windows is driver.switch_to.window().
  2. Step 2: Check the syntax for switching to the second tab

    Use the second handle in the list: driver.window_handles[1].
  3. Final Answer:

    driver.switch_to.window(driver.window_handles[1]) -> Option C
  4. Quick Check:

    Use switch_to.window with window_handles index [OK]
Hint: Use switch_to.window() with window_handles index [OK]
Common Mistakes:
  • Using non-existent methods like switch_window or switch_to_tab
  • Forgetting to use driver.switch_to
  • Using wrong index for window_handles
3. What will the following code print if two tabs are open and the first tab's title is 'Home' and second tab's title is 'About'?
main_window = driver.current_window_handle
all_windows = driver.window_handles
for window in all_windows:
    driver.switch_to.window(window)
    print(driver.title)
medium
A. ["Home", "About"]
B. ["About", "Home"]
C. Only "Home"
D. Only "About"

Solution

  1. Step 1: Understand the loop over all window handles

    The code loops over all open windows and switches to each one in order.
  2. 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.
  3. Final Answer:

    ["Home", "About"] -> Option A
  4. Quick Check:

    Loop prints titles of all tabs in order [OK]
Hint: Loop over window_handles prints all tab titles [OK]
Common Mistakes:
  • Assuming only current tab title prints
  • Mixing order of window handles
  • Not switching before printing title
4. Identify the error in this code snippet that tries to switch back to the main window after opening a new tab:
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())
medium
A. Missing import for switch_to
B. main_window is called like a function with parentheses
C. driver.window_handles[1] is invalid index
D. No error, code is correct

Solution

  1. 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.
  2. Step 2: Identify correct usage to switch back

    Should use driver.switch_to.window(main_window) without parentheses.
  3. Final Answer:

    main_window is called like a function with parentheses -> Option B
  4. Quick Check:

    Window handle is a string, not a function [OK]
Hint: Window handles are strings, don't add () [OK]
Common Mistakes:
  • Calling window handle variables as functions
  • Using wrong index for window_handles
  • Assuming switch_to needs import
5. You open multiple tabs during a test. How do you ensure you always return to the original tab after closing the new ones?
hard
A. Save the original window handle before opening new tabs and switch back using it
B. Use driver.close() on all tabs except the last one
C. Call driver.quit() to close all tabs and restart the browser
D. Switch to the last window handle in driver.window_handles list

Solution

  1. Step 1: Save the original window handle before opening new tabs

    This allows you to remember which tab was the main one.
  2. Step 2: After closing new tabs, switch back to the saved original handle

    Use driver.switch_to.window(original_handle) to return safely.
  3. Final Answer:

    Save the original window handle before opening new tabs and switch back using it -> Option A
  4. Quick Check:

    Save and reuse original handle to return [OK]
Hint: Always save main window handle before opening tabs [OK]
Common Mistakes:
  • Assuming last window handle is original
  • Closing tabs without switching back
  • Using driver.quit() which ends session
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard