How to refresh or reload a webpage in selenium Python

Hello programmers, in this tutorial, we will learn how to refresh or reload a webpage in selenium Python.

Today I’m helping you with how to reload a webpage that is already opened.

To know how to open a URL, you can visit my last tutorial, where I explained briefly how to open a URL using the selenium library. Please visit the given link below.

How to open a URL in Python

Let’s start coding

  • 1st we have to install the selenium library, then import this library and webdriver from the selenium library.
  • Then we have to pass the path of chrome webdriver to the driver.
  • Now pass the URL to the driver which you want to open.
  • At last, we will be able to open the link using the get module.
#installatin of selenium library
pip install selenium
#Importing webdeiver from selenium library
from selenium import webdriver
#path of webdriver(chrome)
path='C:\\Users\\sumit\\.wdm\\drivers\\chromedriver\\win32\\102.0.5005.61\\chromedriver.exe'
#driver 
driver = webdriver.Chrome(path) 
#Url
url="https://www.google.com"
driver.get(url)

output:

How to refresh or reload a webpage in selenium Python

Till now, we opened the link, and now, we want to refresh our page.

  • This can be done easily using the refresh module of the selenium library.
  • So we just added this module after the sleep module, so our page is refreshed after 5 secondsĀ of opening the URL.

let’s see this in the code

#installatin of selenium library 
pip install selenium
#Importing webdeiver from selenium library and time
from selenium import webdriver 
import time
#path of webdriver(chrome) 
path='C:\\Users\\sumit\\.wdm\\drivers\\chromedriver\\win32\\102.0.5005.61\\chromedriver.exe' 
#driver 
driver = webdriver.Chrome(path) 
#Url 
url="https://www.google.com" 
driver.get(url)
time.sleep(5)
#refresh page
driver.refresh()

output:

How to refresh or reload a webpage in selenium Python

Hopefully, you have learned how to refresh or reload a webpage in selenium Python.

Leave a Reply

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