Introduction
Browser history is a record of all the web pages visited during a browser session. Selenium WebDriver allows automation scripts to interact with the browser history using the back() and forward() navigation methods, just like a real user clicking the browser’s Back and Forward buttons.
Testing browser history is important because many web applications depend on proper navigation between pages. Selenium helps verify that users can move backward and forward through previously visited pages without unexpected behavior.
Browser history testing is commonly used in e-commerce websites, banking applications, social media platforms, dashboards, and multi-step web applications.
In this tutorial, you will learn what browser history is, why it is important, how Selenium works with browser history, practical examples, real-world applications, common mistakes, best practices, and frequently asked interview questions.
What is Browser History?
Browser history is the sequence of webpages visited during a browsing session.
Every time a user opens a new page, the browser stores that page in its history. Users can then move backward or forward through the visited pages using the browser’s navigation buttons.
Selenium provides two methods to work with browser history:
driver.back()driver.forward()
These methods simulate the same actions as clicking the browser’s Back and Forward buttons.
Why Do We Use Browser History?
Browser history testing helps to:
Verify browser navigation.
Validate user navigation workflows.
Test multi-page applications.
Ensure browser history works correctly.
Simulate real user browsing behavior.
Test navigation after form submissions or page transitions.
Browser History Methods in Selenium
| Method | Description |
|---|---|
driver.back() | Navigates to the previous page in browser history. |
driver.forward() | Navigates to the next page in browser history. |
Practical Example
The following example demonstrates browser history navigation using Selenium.
The automation script:
Opens three different pages.
Navigates backward through the browser history.
Navigates forward again.
Verifies the current page after each navigation step.
from selenium import webdriver
# Topic: 6. Navigation Commands - Browser History
# Practice site: https://the-internet.herokuapp.com/
#
# Selenium can move through browser history with back() and forward(), similar
# to clicking the browser's back and forward buttons.
def test_browser_history_navigation():
driver = webdriver.Chrome()
try:
pages = [
"https://the-internet.herokuapp.com/",
"https://the-internet.herokuapp.com/login",
"https://the-internet.herokuapp.com/checkboxes",
]
for page in pages:
driver.get(page)
driver.back()
assert driver.current_url.endswith("/login")
driver.back()
assert driver.current_url == "https://the-internet.herokuapp.com/"
driver.forward()
assert driver.current_url.endswith("/login")
finally:
driver.quit()
Output
Browser launched successfully.
Home page opened.
Login page opened.
Checkboxes page opened.
Navigated back to Login page.
Navigated back to Home page.
Navigated forward to Login page.
All Assertions Passed.
Test Executed Successfully.
Understanding the Code
Import WebDriver
from selenium import webdriver
Imports the Selenium WebDriver module.
Launch Chrome Browser
driver = webdriver.Chrome()
Starts a new Chrome browser session.
Store Multiple URLs
pages = [
"https://the-internet.herokuapp.com/",
"https://the-internet.herokuapp.com/login",
"https://the-internet.herokuapp.com/checkboxes",
]
Creates a list of pages that will be visited during the test.
Visit All Pages
for page in pages:
driver.get(page)
Opens each page one after another.
Each visited page is automatically added to the browser history.
Navigate Back
driver.back()
Moves to the previously visited page (Login page).
Verify Current Page
assert driver.current_url.endswith("/login")
Verifies that Selenium successfully navigated back to the Login page.
Navigate Back Again
driver.back()
Moves back to the Home page.
Verify Home Page
assert driver.current_url == "https://the-internet.herokuapp.com/"
Confirms that the browser returned to the Home page.
Navigate Forward
driver.forward()
Moves forward to the Login page.
Verify Login Page Again
assert driver.current_url.endswith("/login")
Confirms that browser history navigation is working correctly.
Close the Browser
driver.quit()
Ends the WebDriver session and closes all browser windows.
Browser History Navigation Flow
Launch Browser
│
▼
Open Home Page
│
▼
Open Login Page
│
▼
Open Checkboxes Page
│
▼
Navigate Back
│
▼
Return to Login Page
│
▼
Navigate Back
│
▼
Return to Home Page
│
▼
Navigate Forward
│
▼
Return to Login Page
│
▼
Verify Navigation
│
▼
Close Browser
Automation Testing Example
Suppose you’re testing an online shopping website.
A customer:
Opens the Home page.
Visits a Product page.
Opens the Shopping Cart.
Clicks the browser’s Back button.
Returns to the Product page.
Clicks Back again.
Returns to the Home page.
Clicks Forward.
Returns to the Product page.
Selenium can automate and verify this entire navigation flow using browser history commands.
Real-World Example
Consider an online banking application.
A user:
Opens the Dashboard.
Navigates to Transaction History.
Opens Account Details.
Uses the browser’s Back button twice.
Returns to the Dashboard.
Uses the Forward button.
Returns to Transaction History.
An automation script can verify that browser history behaves exactly as expected.
Common Mistakes Beginners Make
Expecting Browser History Without Navigation
Browser history exists only after visiting multiple pages.
driver.get("https://the-internet.herokuapp.com/")
driver.back()
If no previous page exists, nothing happens.
Forgetting to Validate Navigation
Always verify the page after calling back() or forward().
assert driver.current_url.endswith("/login")
Confusing Browser History with get()
Some beginners think:
driver.get(previous_url)
is the same as:
driver.back()
It is not.
get() loads a specific URL.
back() navigates using the browser history.
Using Hardcoded Delays
Avoid:
time.sleep(5)
Instead, use Selenium Explicit Waits whenever synchronization is required.
Best Practices
Test browser history after visiting multiple pages.
Verify navigation using
current_urlortitle.Use assertions after every navigation step.
Prefer explicit waits on slow-loading pages.
Simulate realistic user navigation workflows.
Always close the browser using
driver.quit().
Conclusion
Browser history plays an important role in web application navigation. Selenium WebDriver allows automation engineers to test browser history using the back() and forward() methods, ensuring that users can navigate between previously visited pages just like they would in a real browser.
Understanding browser history navigation helps build more realistic, reliable, and user-focused Selenium automation tests.
Frequently Asked Questions (FAQs)
What is browser history?
Browser history is the list of webpages visited during a browsing session.
Which Selenium methods work with browser history?
driver.back()driver.forward()
Does get() use browser history?
No.
The get() method directly opens the specified URL and does not navigate through browser history.
Can Selenium verify browser history?
Yes.
You can verify browser history navigation using:
driver.current_urldriver.titlePage elements
Why is browser history testing important?
It ensures that users can navigate backward and forward correctly, providing a smooth and reliable browsing experience.
Key Takeaways
Browser history stores the sequence of visited webpages.
Selenium uses
driver.back()anddriver.forward()to navigate through browser history.Browser history testing verifies real user navigation behavior.
Always validate navigation using
current_url,title, or page elements.Browser history is commonly tested in multi-page web applications.
Use assertions after every navigation step for reliable automation.
Browser history is an important Selenium interview topic and an essential part of browser automation testing.
