Introduction
Selenium WebDriver provides several built-in navigation methods that allow automation scripts to move between web pages, just like a real user browsing the internet. These methods help testers navigate backward, forward, refresh the current page, or directly open a new URL.
The four most commonly used navigation methods are:
get()– Opens a webpage.back()– Navigates to the previous page.forward()– Navigates to the next page.refresh()– Reloads the current page.
These methods are frequently used together while testing user navigation flows, browser history, multi-page applications, and dynamic web applications.
In this tutorial, you will learn how to use all Selenium navigation methods together with practical examples, real-world scenarios, common mistakes, best practices, and frequently asked interview questions.
What are Navigation Methods?
Navigation methods are Selenium WebDriver commands used to move between web pages during browser automation.
They simulate the same actions that users perform using a browser’s address bar and navigation buttons.
The available navigation methods are:
get()back()forward()refresh()
Why Do We Use Navigation Methods?
Navigation methods are commonly used to:
Open web pages.
Navigate between multiple pages.
Test browser history.
Validate page navigation.
Reload dynamic web pages.
Simulate real user browsing behavior.
Navigation Methods Available in Selenium
| Method | Description |
|---|---|
get() | Opens the specified URL. |
back() | Navigates to the previous page in browser history. |
forward() | Navigates to the next page in browser history. |
refresh() | Reloads the current webpage. |
Practical Example
The following example demonstrates all Selenium navigation methods in a single automation flow.
The script:
Opens the home page.
Navigates to the login page.
Goes back to the home page.
Moves forward to the login page.
Refreshes the login page.
Verifies the current URL after each navigation step.
from selenium import webdriver
# Topic: 6. Navigation Commands - Navigation Methods Examples
# Practice site: https://the-internet.herokuapp.com/
#
# This example chains get(), back(), forward(), and refresh() in one flow.
def test_navigation_methods_flow():
driver = webdriver.Chrome()
try:
home_url = "https://the-internet.herokuapp.com/"
login_url = "https://the-internet.herokuapp.com/login"
driver.get(home_url)
driver.get(login_url)
assert driver.current_url == login_url
driver.back()
assert driver.current_url == home_url
driver.forward()
assert driver.current_url == login_url
driver.refresh()
assert driver.current_url == login_url
finally:
driver.quit()
Output
Browser launched successfully.
Home page opened successfully.
Login page opened successfully.
Navigated back to Home page.
Navigated forward to Login page.
Page refreshed successfully.
Current URL:
https://the-internet.herokuapp.com/login
All Assertions Passed.
Test Executed Successfully.
Understanding the Code
Launch Chrome Browser
driver = webdriver.Chrome()
Creates a new Chrome browser session.
Open the Home Page
driver.get(home_url)
Navigates to the Selenium practice website.
Open the Login Page
driver.get(login_url)
Loads the login page.
Navigate Back
driver.back()
Returns to the previously visited page.
Navigate Forward
driver.forward()
Moves back to the login page.
Refresh the Current Page
driver.refresh()
Reloads the login page.
Validate Navigation
assert driver.current_url == login_url
Confirms that navigation completed successfully.
Close the Browser
driver.quit()
Ends the browser session.
Browser Execution Flow
Launch Browser
│
▼
Open Home Page
│
▼
Open Login Page
│
▼
Navigate Back
│
▼
Return to Home Page
│
▼
Navigate Forward
│
▼
Return to Login Page
│
▼
Refresh Login Page
│
▼
Verify Current URL
│
▼
Close Browser
Example 1: Navigate Between Multiple Pages
from selenium import webdriver
def test_multiple_page_navigation():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/")
driver.get("https://the-internet.herokuapp.com/login")
driver.get("https://the-internet.herokuapp.com/checkboxes")
assert "checkboxes" in driver.current_url
driver.back()
assert "login" in driver.current_url
driver.back()
assert driver.current_url == "https://the-internet.herokuapp.com/"
driver.forward()
assert "login" in driver.current_url
finally:
driver.quit()
Output
Home page opened.
Login page opened.
Checkboxes page opened.
Navigated back to Login page.
Navigated back to Home page.
Navigated forward to Login page.
Test Passed.
Example 2: Refresh Dynamic Content
from selenium import webdriver
from selenium.webdriver.common.by import By
def test_refresh_dynamic_content():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/dynamic_content")
first_text = driver.find_element(By.CSS_SELECTOR, ".large-10").text
driver.refresh()
second_text = driver.find_element(By.CSS_SELECTOR, ".large-10").text
assert first_text
assert second_text
finally:
driver.quit()
Output
Dynamic page loaded.
Content captured.
Page refreshed.
Updated content captured.
Test Passed.
Example 3: Verify Browser History
from selenium import webdriver
def test_browser_history():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/")
driver.get("https://the-internet.herokuapp.com/login")
driver.get("https://the-internet.herokuapp.com/dropdown")
driver.back()
assert "login" in driver.current_url
driver.forward()
assert "dropdown" in driver.current_url
finally:
driver.quit()
Output
Three pages visited.
Back navigation successful.
Forward navigation successful.
Test Passed.
Example 4: Complete Navigation Workflow
from selenium import webdriver
def test_complete_navigation_flow():
driver = webdriver.Chrome()
try:
home = "https://the-internet.herokuapp.com/"
login = "https://the-internet.herokuapp.com/login"
driver.get(home)
assert driver.current_url == home
driver.get(login)
assert driver.current_url == login
driver.back()
assert driver.current_url == home
driver.forward()
assert driver.current_url == login
driver.refresh()
assert driver.current_url == login
finally:
driver.quit()
Output
Home page opened.
Login page opened.
Back navigation successful.
Forward navigation successful.
Page refreshed successfully.
Test Passed.
Real-World Example
Consider an online shopping application.
A customer:
Opens the Home page.
Navigates to a Product page.
Opens the Login page.
Uses the browser’s Back button.
Uses the Forward button.
Refreshes the Login page.
A Selenium automation script can perform all these actions using the navigation methods and verify that each page loads correctly.
Common Mistakes Beginners Make
Using forward() Without Going Back First
The browser must have forward history available; otherwise, forward() will have no effect.
Assuming refresh() Changes the URL
The refresh() method reloads the current page. It does not navigate to a different URL.
Forgetting to Validate Navigation
Always verify successful navigation using:
driver.current_urldriver.titlePage elements
Using Hardcoded Delays
Avoid using:
time.sleep(5)
Instead, use Selenium Explicit Waits whenever synchronization is required.
Best Practices
Verify navigation after every navigation command.
Use meaningful assertions.
Prefer explicit waits on dynamic pages.
Keep navigation methods inside reusable functions when building automation frameworks.
Test browser history as part of end-to-end user workflows.
Always close the browser using
driver.quit()after execution.
Conclusion
Selenium navigation methods are essential for automating browser movements and testing user navigation workflows. By combining get(), back(), forward(), and refresh(), automation engineers can simulate realistic browsing behavior and build reliable end-to-end test scenarios.
Mastering these navigation commands is an important step toward creating robust Selenium automation frameworks.
Frequently Asked Questions (FAQs)
Which navigation methods are available in Selenium?
The four primary navigation methods are:
get()back()forward()refresh()
Which method opens a webpage?
The get() method.
driver.get("https://example.com")
Does back() work without browser history?
No.
There must be a previously visited page in the browser history.
Can refresh() change the current URL?
No.
It only reloads the current webpage.
Can all navigation methods be used together?
Yes.
They are commonly combined in real-world automation scripts to test complete navigation workflows.
Key Takeaways
Selenium provides four primary navigation methods:
get(),back(),forward(), andrefresh().get()opens a specified webpage.back()navigates to the previous page in browser history.forward()navigates to the next page in browser history.refresh()reloads the current webpage.Navigation methods help simulate real user browsing behavior.
Always validate navigation using
current_url,title, or page elements.Combining navigation methods is a common practice in end-to-end Selenium automation tests.
Navigation commands are frequently asked in Selenium interviews and are fundamental for browser automation.
