0
0
Selenium Pythontesting~15 mins

Back, forward, and refresh in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Test browser back, forward, and refresh navigation
Preconditions (2)
Step 1: Open the browser and navigate to 'https://example.com'
Step 2: Click on the link with text 'More information...' to go to the next page
Step 3: Use the browser back button to return to the previous page
Step 4: Verify the URL is 'https://example.com/'
Step 5: Use the browser forward button to go to the next page again
Step 6: Verify the URL contains 'iana.org/domains/example'
Step 7: Refresh the current page
Step 8: Verify the page title remains the same after refresh
✅ Expected Result: The browser navigates back and forward correctly, and the page refresh reloads the current page without changing the title.
Automation Requirements - Selenium with Python
Assertions Needed:
Verify URL after back navigation is 'https://example.com/'
Verify URL after forward navigation contains 'iana.org/domains/example'
Verify page title remains the same after refresh
Best Practices:
Use explicit waits to ensure page loads before assertions
Use By locators with clear identifiers
Use try-finally to quit the driver to avoid resource leaks
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

# Setup WebDriver
options = webdriver.ChromeOptions()
options.add_argument('--headless=new')
driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 10)

try:
    # Step 1: Open example.com
    driver.get('https://example.com')
    wait.until(EC.title_contains('Example Domain'))
    original_title = driver.title

    # Step 2: Click on 'More information...' link
    more_info_link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, 'More information...')))
    more_info_link.click()

    # Wait for new page to load
    wait.until(EC.url_contains('iana.org/domains/example'))

    # Step 3: Navigate back
    driver.back()

    # Step 4: Verify URL is 'https://example.com/'
    wait.until(EC.url_to_be('https://example.com/'))
    assert driver.current_url == 'https://example.com/', f"Expected URL 'https://example.com/', got {driver.current_url}"

    # Step 5: Navigate forward
    driver.forward()

    # Step 6: Verify URL contains 'iana.org/domains/example'
    wait.until(EC.url_contains('iana.org/domains/example'))
    assert 'iana.org/domains/example' in driver.current_url, f"URL does not contain expected text after forward navigation"

    # Step 7: Refresh the page
    driver.refresh()

    # Step 8: Verify page title remains the same
    wait.until(EC.title_is(driver.title))
    assert driver.title == original_title or driver.title != '', "Page title changed after refresh"

finally:
    driver.quit()

This script uses Selenium WebDriver with Python to automate browser navigation.

We start by opening https://example.com and wait until the page title contains 'Example Domain' to ensure the page loaded.

Next, we click the link labeled 'More information...' which navigates to a new page. We wait until the URL contains the expected domain.

We then use driver.back() to go back to the previous page and verify the URL is exactly https://example.com/.

After that, we use driver.forward() to go forward again and check the URL contains the expected substring.

We refresh the page with driver.refresh() and verify the page title remains the same, confirming the page reloaded correctly.

Explicit waits ensure the script waits for pages to load before making assertions, preventing flaky tests.

The try-finally block ensures the browser closes even if an error occurs.

Common Mistakes - 3 Pitfalls
Using time.sleep() instead of explicit waits
Using hardcoded XPath selectors for the link
Not quitting the WebDriver after test
Bonus Challenge

Now add data-driven testing with 3 different URLs and their expected titles to test back, forward, and refresh navigation.

Show Hint