Back, forward, and refresh 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 # 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.
Now add data-driven testing with 3 different URLs and their expected titles to test back, forward, and refresh navigation.
Practice
driver.back() command do in Selenium?Solution
Step 1: Understand the purpose of
This command simulates clicking the browser's back button, which moves to the previous page in the browser history.driver.back()Step 2: Compare with other navigation commands
driver.forward()moves forward,driver.refresh()reloads the page, and closing the browser is done bydriver.close().Final Answer:
It navigates the browser to the previous page in history. -> Option BQuick Check:
Back button = driver.back() [OK]
- Confusing back() with refresh()
- Thinking back() closes the browser
- Mixing back() with forward()
Solution
Step 1: Recall Selenium WebDriver refresh syntax in Python
The correct method to refresh the page isdriver.refresh().Step 2: Identify incorrect options
driver.reload()anddriver.page_refresh()do not exist in Selenium Python API.driver.navigate().refresh()is Java syntax, not Python.Final Answer:
driver.refresh() -> Option DQuick Check:
Refresh page = driver.refresh() [OK]
- Using Java syntax in Python code
- Trying non-existent methods like reload()
- Confusing refresh with back or forward
driver.get('https://example.com/page1')
driver.get('https://example.com/page2')
driver.back()
current_url = driver.current_url
print(current_url)Solution
Step 1: Understand navigation commands
The code first opens page1, then page2, then goes back to the previous page usingdriver.back().Step 2: Determine current URL after back()
After going back, the browser is on page1, sodriver.current_urlreturns 'https://example.com/page1'.Final Answer:
https://example.com/page1 -> Option CQuick Check:
Back goes to previous page URL [OK]
- Assuming current URL stays on page2
- Confusing back() with refresh()
- Expecting an error instead of URL
driver.get('https://example.com')
driver.back
print(driver.current_url)Solution
Step 1: Check method call syntax
driver.backis missing parentheses, so it does not execute the method.Step 2: Verify other parts
driver.get()is correct,driver.current_urlis the right property, and order of calls is valid.Final Answer:
Missing parentheses after back method call -> Option AQuick Check:
Method calls need () in Python [OK]
- Forgetting parentheses on method calls
- Using wrong property for URL
- Misordering navigation commands
Solution
Step 1: Understand the test goal
The goal is to check if data reloads on page refresh, so the test must refresh the current page.Step 2: Match commands to behavior
driver.refresh()reloads the page.driver.back()anddriver.forward()navigate history, not refresh.driver.close()closes the browser, not reload.Final Answer:
driver.get(url); driver.refresh(); verify data reload -> Option AQuick Check:
Refresh page = driver.refresh() [OK]
- Using back() or forward() instead of refresh()
- Closing browser instead of refreshing
- Not verifying after refresh
Ready to prove it?
Pass the test and this pattern moves unknown
