How to refresh a browser in Selenium Python

In this post, we will learn How to refresh a browser in Selenium Python. The Selenium web driver API is used for test automation on Windows or mobile. It helps us remotely control different browsers automatically as if a real user is interacting by stimulating activities like opening or closing a new window, or tab, entering text in the textbox, selecting values from the dropdown, and mouse control.

1. How to refresh a browser in Selenium Python


In this example, We are navigating to a webpage using a driver. get() method.We are launching the Chrome browser with the help of  ChromeDriverManager and driver. refresh() to refresh the already open web page. Once the webpage load The refresh of the page happened so quickly that we can’t notice it. We will use the sleep() method in the next example.

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time

driver = webdriver.Chrome(ChromeDriverManager().install())


driver.get("https://accounts.google.com/") 

print("The First opened window  = " + driver.title)

driver.refresh()
  

driver.close()
driver.quit()

Output

Sign in - Google Accounts

2. How to refresh a browser in Selenium Python


The selenium web driver API method driver.refresh() method is used. We are launching the Chrome browser with the help of  ChromeDriverManager and navigating to the URL by the broad driver.get(“https://accounts.google.com/”) method and wait for 2 seconds and refresh the Page.

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time

driver = webdriver.Chrome(ChromeDriverManager().install())


driver.get("https://accounts.google.com/") 

print("The First opened window  = " + driver.title)

time.sleep(4) 

driver.refresh()
  
time.sleep(4)


driver.close()
driver.quit()

Output

Sign in - Google Accounts