Python selenium update page

How to Refresh the Page with Selenium

Refreshing webpages is a very common action. We refresh the webpage to see the updated results. The same thing is true for browser testing, web automation, and web scraping with Selenium web driver.

In this article, I am going to show you how to refresh a page with the Selenium Python library. So, let’s get started.

Prerequisites:

To try out the commands and examples of this article, you must have,

1) A Linux distribution (preferably Ubuntu) installed on your computer.
2) Python 3 installed on your computer.
3) PIP 3 installed on your computer.
4) Python virtualenv package installed on your computer.
5) Mozilla Firefox or Google Chrome web browsers installed on your computer.
6) Must know how to install the Firefox Gecko Driver or Chrome Web Driver.

For fulfilling the requirements 4, 5, and 6, read my article Introduction to Selenium with Python 3 at Linuxhint.com.

You can find many articles on the other topics on LinuxHint.com. Be sure to check them out if you need any assistance.

Setting Up a Project Directory:

To keep everything organized, create a new project directory selenium-refresh/ as follows:

Navigate to the selenium-refresh/ project directory as follows:

Create a Python virtual environment in the project directory as follows:

Activate the virtual environment as follows:

Install Selenium Python library using PIP3 as follows:

Download and install all the required web driver in the drivers/ directory of the project. I have explained the process of downloading and installing web drivers in my article Introduction to Selenium with Python 3. If you need any assistance, search on LinuxHint.com for that article.

Method 1: Using the refresh() Browser Method

The first method is the easiest and the recommended method of the refreshing page with Selenium.

Create a new Python script ex01.py in and type in the following lines of codes in it.

Читайте также:  Python with open file example

from selenium import webdriver
from selenium. webdriver . common . keys import Keys
from time import sleep
options = webdriver. ChromeOptions ( )
options. headless = True
browser = webdriver. Chrome ( executable_path = «./drivers/chromedriver» , options = options )
browser. get ( «https://www.unixtimestamp.com/» )
timestamp = browser. find_element_by_xpath ( «//h3[@class=’text-danger’][1]» )
print ( ‘Current timestamp: %s’ % ( timestamp. text . split ( ‘ ‘ ) [ 0 ] ) )
sleep ( 5 )
browser. refresh ( )
timestamp = browser. find_element_by_xpath ( «//h3[@class=’text-danger’][1]» )
print ( ‘Current timestamp: %s’ % ( timestamp. text . split ( ‘ ‘ ) [ 0 ] ) )
browser. close ( )

Once you’re done, save the ex01.py Python script.

Line 1 and 2 imports all the required Selenium components.

Line 3 imports sleep() function from time library. I will use this to wait a few seconds for the webpage to update so that we can fetch new data after refreshing the webpage.

Line 5 creates a Chrome Options object, and line 6 enables headless mode for the Chrome web browser.

Line 8 creates a Chrome browser object using the chromedriver binary from the drivers/ directory of the project.

Line 9 tells the browser to load the website unixtimestamp.com.

Line 11 finds the element that has the timestamp data from the page using the XPath selector and stores it in the timestamp variable.

Line 12 parses the timestamp data from the element and prints it on the console.

Line 14 uses the sleep() function to wait for 5 seconds.

Line 15 refreshes the current page using the browser.refresh() method.

Line 17 and 18 is the same as line 11 and 12. It finds the timestamp element from the page and prints the updated timestamp on the console.

Line 20 closes the browser.

Run the Python script ex01.py as follows:

As you can see, the timestamp is printed on the console.

After 5 seconds of printing the first timestamp, the page is refreshed, and the updated timestamp is printed on the console, as you can see in the screenshot below.

Method 2: Revisiting the Same URL

The second method of refresh the page is to revisit the same URL using the browser.get() method.

Create a Python script ex02.py in your project directory and type in the following lines of codes in it.

from selenium import webdriver
from selenium. webdriver . common . keys import Keys
from time import sleep
options = webdriver. ChromeOptions ( )
options. headless = True
browser = webdriver. Chrome ( executable_path = «./drivers/chromedriver» , options = options )
browser. get ( «https://www.unixtimestamp.com/» )
timestamp = browser. find_element_by_xpath ( «//h3[@class=’text-danger’][1]» )
print ( ‘Current timestamp: %s’ % ( timestamp. text . split ( ‘ ‘ ) [ 0 ] ) )
sleep ( 5 )
browser. get ( browser. current_url )
timestamp = browser. find_element_by_xpath ( «//h3[@class=’text-danger’][1]» )
print ( ‘Current timestamp: %s’ % ( timestamp. text . split ( ‘ ‘ ) [ 0 ] ) )
browser. close ( )

Читайте также:  Incorrect Concept of Onclick Event

Once you’re done, save the ex02.py Python script.

Everything is the same as in ex01.py. The only difference is in line 15.

Here, I am using the browser.get() method to visit the current page URL. The current page URL can be accessed using the browser.current_url property.

Run the ex02.py Python script as follows:

As you can see, the Pythion script ex02.py prints the same type of information as in ex01.py.

Conclusion:

In this article, I have shown you 2 methods of refreshing the current webpage using the Selenium Python library. You should be able to do more interesting things with Selenium now.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.

Источник

Python Selenium Refresh Page

Python Selenium Refresh Page

  1. Install Selenium in Python
  2. Use Selenium to Refresh a Web Page

Selenium is one of the most powerful tools for web automation, and it is functional for almost all browsers and major operating systems (OS) like Windows, macOS, and Linux. In this article, we will learn how to use Selenium to refresh a webpage in Python.

Install Selenium in Python

To install Selenium for Python on your local machine, you can use the famous pip command in Command Line Interface (CLI) as follow.

The above command will download and install Selenium. Once it’s done, you can import Selenium into your Python program.

Let’s check the version of Selenium to verify that it’s successfully installed on our machines.

import selenium print(selenium.__version__) 

Use Selenium to Refresh a Web Page

We can use Selenium to refresh a web page and do many other automated tasks, but this article focuses on using Selenium to refresh a web page in Python. First, let’s download the necessary webdrivers.

In Python, Selenium requires a driver to interact with the selected browser, and it is necessary to install the drivers so you provide its path in the code. For this article, I’ll be using Chrome as a browser and downloading the drivers for it.

driver = webdriver.Chrome(executable_path=r"D:\chromedriver.exe") 

You can see in the above example snippet that I have chosen my browser as Chrome and provided it the driver’s path (executable_path=r»D:\chromedriver.exe») .

from selenium import webdriver  # set the chromodriver.exe path driver = webdriver.Chrome(executable_path=r"D:\chromedriver.exe")  # launch URL driver.get("https://www.delftstack.com/")  #crefresh page driver.refresh()  # close driver.close() 

Use selenium to refresh a web page in Python

The above code will pop up a new, redirecting you to the provided URL as follow.

driver.get("https://www.delftstack.com/" 

Displayed a message on the header bar of Chrome saying that Chrome is being controlled by automated test software , which is nothing but Selenium.

Читайте также:  Input type php integer

Zeeshan is a detail oriented software engineer that helps companies and individuals make their lives and easier with software solutions.

Related Article — Python Selenium

Источник

How to refresh a webpage using python selenium webdriver

  • All categories
  • ChatGPT (11)
  • Apache Kafka (84)
  • Apache Spark (596)
  • Azure (145)
  • Big Data Hadoop (1,907)
  • Blockchain (1,673)
  • C# (141)
  • C++ (271)
  • Career Counselling (1,060)
  • Cloud Computing (3,469)
  • Cyber Security & Ethical Hacking (162)
  • Data Analytics (1,266)
  • Database (855)
  • Data Science (76)
  • DevOps & Agile (3,608)
  • Digital Marketing (111)
  • Events & Trending Topics (28)
  • IoT (Internet of Things) (387)
  • Java (1,247)
  • Kotlin (8)
  • Linux Administration (389)
  • Machine Learning (337)
  • MicroStrategy (6)
  • PMP (423)
  • Power BI (516)
  • Python (3,193)
  • RPA (650)
  • SalesForce (92)
  • Selenium (1,569)
  • Software Testing (56)
  • Tableau (608)
  • Talend (73)
  • TypeSript (124)
  • Web Development (3,002)
  • Ask us Anything! (66)
  • Others (2,231)
  • Mobile Development (395)
  • UI UX Design (24)

Join the world’s most active Tech Community!

Welcome back to the World’s most active Tech Community!

Subscribe to our Newsletter, and get personalized recommendations.

GoogleSign up with Google facebookSignup with Facebook

Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

  • DevOps Certification Training
  • AWS Architect Certification Training
  • Big Data Hadoop Certification Training
  • Tableau Training & Certification
  • Python Certification Training for Data Science
  • Selenium Certification Training
  • PMP® Certification Exam Training
  • Robotic Process Automation Training using UiPath
  • Apache Spark and Scala Certification Training
  • Microsoft Power BI Training
  • Online Java Course and Training
  • Python Certification Course
  • Data Scientist Masters Program
  • DevOps Engineer Masters Program
  • Cloud Architect Masters Program
  • Big Data Architect Masters Program
  • Machine Learning Engineer Masters Program
  • Full Stack Web Developer Masters Program
  • Business Intelligence Masters Program
  • Data Analyst Masters Program
  • Test Automation Engineer Masters Program
  • Post-Graduate Program in Artificial Intelligence & Machine Learning
  • Post-Graduate Program in Big Data Engineering

COMPANY

WORK WITH US

DOWNLOAD APP

appleplaystore googleplaystore

CATEGORIES

CATEGORIES

  • Cloud Computing
  • DevOps
  • Big Data
  • Data Science
  • BI and Visualization
  • Programming & Frameworks
  • Software Testing © 2023 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved. Terms & ConditionsLegal & Privacy

Источник

Оцените статью