back()

Introduction

The back() method is one of the navigation commands in Selenium WebDriver. It is used to navigate back to the previous page in the browser’s history, just like clicking the Back button in a web browser.

Whenever users browse a website, the browser maintains a history of visited pages. The back() method allows Selenium to move to the previous page in that history without manually entering the URL again.

The back() method is widely used in automation testing to verify page navigation, browser history behavior, breadcrumb links, and user workflows that involve moving between multiple pages.

In this tutorial, you will learn what the back() method is, why it is used, its syntax, practical examples, real-world applications, common mistakes, best practices, and frequently asked interview questions.


What is back()?

The back() method is a Selenium WebDriver navigation command that moves the browser to the previously visited page in the current browser session.

It performs the same action as clicking the Back button available in every web browser.

Unlike the get() method, which loads a specific URL, the back() method relies on the browser’s navigation history.


Why Do We Use back()?

The back() method is commonly used to:

  • Navigate to the previous page.

  • Test browser history functionality.

  • Verify navigation workflows.

  • Validate breadcrumb navigation.

  • Test multi-page web applications.

  • Simulate real user browsing behavior.


Syntax

driver.back()

The back() method does not accept any parameters.


Practical Example

The following example demonstrates how to use the back() method.

The automation script opens the Selenium practice website, navigates to the login page, then uses the back() method to return to the home page and verifies that the browser has successfully navigated back.

from selenium import webdriver


# Topic: 6. Navigation Commands - back()
# Practice site: https://the-internet.herokuapp.com/
#
# back() moves the browser to the previous page in the session history.


def test_navigate_back():
    driver = webdriver.Chrome()

    try:
        driver.get("https://the-internet.herokuapp.com/")
        driver.get("https://the-internet.herokuapp.com/login")

        driver.back()

        assert driver.current_url == "https://the-internet.herokuapp.com/"

    finally:
        driver.quit()

Output

Browser launched successfully.

Home page opened successfully.

Login page opened successfully.

Browser navigated back to the previous page.

Current URL:
https://the-internet.herokuapp.com/

Assertion Passed.

Test Executed Successfully.

Understanding the Code

Import WebDriver

from selenium import webdriver

Imports the Selenium WebDriver module required to automate the browser.


Launch Chrome Browser

driver = webdriver.Chrome()

Creates a new Chrome browser session.


Open the Home Page

driver.get("https://the-internet.herokuapp.com/")

Opens the Selenium practice website.


Navigate to the Login Page

driver.get("https://the-internet.herokuapp.com/login")

Navigates to the login page.

At this point, the browser history contains two pages.


Navigate Back

driver.back()

Moves the browser back to the previously visited page in the browser history.

This behaves exactly like clicking the browser’s Back button.


Verify the Previous Page

assert driver.current_url == "https://the-internet.herokuapp.com/"

Checks that the browser has successfully returned to the home page.

If the URL matches, the assertion passes.


Close the Browser

driver.quit()

Closes the browser and ends the WebDriver session.


Browser Execution Flow

Launch Browser
        │
        ▼
Open Home Page
        │
        ▼
Open Login Page
        │
        ▼
Execute back()
        │
        ▼
Browser Returns to Previous Page
        │
        ▼
Verify Current URL
        │
        ▼
Assertion Passes
        │
        ▼
Close Browser

Automation Testing Example

Suppose you’re testing an e-commerce website.

A customer:

  • Opens the Home page.

  • Navigates to a product page.

  • Clicks the browser’s Back button.

The automation script can verify that the browser correctly returns to the product listing page.

driver.back()

assert "products" in driver.current_url

This confirms that browser history navigation works correctly.


Real-World Example

Consider an online banking application.

A customer:

  • Opens the Dashboard.

  • Navigates to the Transaction History page.

  • Uses the browser’s Back button.

The automation script can verify that the customer is returned to the Dashboard page.

driver.back()

assert "dashboard" in driver.current_url

This ensures that navigation behaves as expected.


Common Mistakes Beginners Make

Calling back() Without Browser History

Incorrect

driver.get("https://the-internet.herokuapp.com/")

driver.back()

If only one page has been visited, the browser has no previous page to navigate back to.


Verifying the URL Too Quickly

Incorrect

driver.back()

assert driver.current_url == expected_url

On slow websites, the previous page may still be loading.

Use explicit waits whenever necessary before validating the page.


Confusing back() with get()

Some beginners use:

driver.get(previous_url)

instead of:

driver.back()

get() opens a specified URL, while back() navigates using the browser’s history.


Best Practices

  • Use back() only when browser history exists.

  • Verify navigation using current_url or title.

  • Use explicit waits for pages that load slowly.

  • Combine navigation commands with assertions for reliable automation.

  • Use meaningful test cases that simulate real user navigation.


Conclusion

The back() method is an essential Selenium navigation command used to move the browser to the previously visited page in the current session.

It behaves exactly like clicking the browser’s Back button and is widely used for testing browser history, navigation workflows, and real-world user scenarios.

Understanding how to use the back() method is fundamental for creating reliable Selenium automation scripts.


Frequently Asked Questions (FAQs)

What does the back() method do?

It navigates the browser to the previous page in the browser history.


Does back() require any parameters?

No.

The syntax is:

driver.back()

Is back() the same as clicking the browser’s Back button?

Yes.

It performs exactly the same action.


Can back() be used if only one page has been opened?

No.

If there is no previous page in the browser history, the browser cannot navigate back.


How can I verify that back() worked?

You can validate the navigation using:

  • driver.current_url

  • driver.title

  • Page elements


Key Takeaways

  • The back() method navigates to the previous page in the browser history.

  • The syntax is driver.back().

  • It performs the same action as clicking the browser’s Back button.

  • It does not accept any parameters.

  • It relies on the browser’s navigation history.

  • Use current_url or title to verify successful navigation.

  • The back() method is a frequently asked Selenium interview topic and an essential browser navigation command.