How to refresh a webpage using Selenium WebDriver

Introduction

Selenium is a powerful tool to programmatically manipulate an internet browser. It supports all major browsers, runs on all major operating systems, and has scripts written in many different languages, including Python, Java, C#, and others.

An interface called Selenium WebDriver enables us to run tests on browsers. It enables us to write test scripts in the programming language of our choice.

Installation

Note: Before installing the selenium web bindings, run the following command to ensure that Python has been installed.

python --version

To install the selenium web bindings using pip, run the following command:

pip install selenium

The refresh() method

The refresh() method is used to refresh the currrent page in selenium.

Syntax

driver.refresh()

Code example

Let’s look at the code below:

from selenium import webdriver
driver = webdriver.Firefox(executable_path="path to the driver")
driver.get("https://educative.io/")
driver.refresh()
driver.close()

Code explanation

  • Line 1: We import webdriver from the selenium module.
  • Line 3: We use the Firefox driver here. A driver object is created by invoking the Firefox() method. The path to the driver is specified. The driver can be downloaded from here.
  • Line 4: We retrieve the https://educative.io web page
  • Line 5: We refresh the webpage using the refresh() method.
  • Line 6: We close the driver by invoking the close() method.

Free Resources