How to handle the browsing history in Selenium?

How to handle the browsing history in Selenium?

In this article we will discuss how to handle the web browsing history in selenium.

In Browser toolbar, we can do back, forward, and refresh. Therefore, in this case study, we will demo how back, forward, and refresh can be done in selenium.


Key API in Selenium

  • driver.back()
  • driver.forward()
  • driver.refresh()

Here are the key Selenium API we will use to do browsing history back, forward, and refresh.

Testing Scenario

  1. Visit
  2. Search “selenium webdriver“
  3. Click one of search results, “Selenium WebDriver”
  4. Back
  5. Search Title = “selenium webdriver – Google Search”
  6. Forward
  7. Search “Selenium WebDriver”
  8. Refresh
  9. Search “Selenium WebDriver”


Sample Code

[pastacode lang=”python” message=”Browsing History Handling in Selenium” highlight=”37,47,53″ provider=”manual”]

import unittest
from selenium import webdriver
from import WebDriverWait
from import expected_conditions
class NavigationTest(unittest.TestCase):
    def setUp(self):
        # create a new Firefox session
        self.driver = webdriver.Chrome()

        # navigate to the application home page

    def testBrowserNavigation(self):
        driver = self.driver
        # get the search textbox
        search_field = driver.find_element_by_name("q")
        # enter search keyword and submit
        search_field.send_keys("selenium webdriver")
        # click the SElenium WebDriver link
        se_wd_link = driver.find_element_by_link_text("Selenium WebDriver")
        # Verify if the target window title is "Selenium WebDriver"
        self.assertEqual("Selenium WebDriver", driver.title)
        # Click Back to previous page. It's google search result in this case.

        # Verify if it's google search results with title "Selenium WebDriver - Google"
        self.assertTrue(WebDriverWait(self.driver, 30).until(expected_conditions.title_contains("Selenium WebDriver - Google")))

        # Click Forward. It should be the Selenium WebDriver page.
        self.assertTrue(WebDriverWait(self.driver, 10).until(expected_conditions.title_is("Selenium WebDriver")))

        # Click Refresh to load the page again.
        self.assertTrue(WebDriverWait(self.driver, 10).until(expected_conditions.title_is("Selenium WebDriver")))

    def tearDown(self):
        # close the browser window

if __name__ == '__main__':



Leave a Reply

Your email address will not be published. Required fields are marked *