Stipulations:
To check out the instructions and examples of this text, it’s essential to have,
1) A Linux distribution (ideally Ubuntu) put in in your pc.
2) Python Three put in in your pc.
3) PIP Three put in in your pc.
4) Python virtualenv package deal put in in your pc.
5) Mozilla Firefox or Google Chrome net browsers put in in your pc.
6) Should know the right way to set up the Firefox Gecko Driver or Chrome Net Driver.
For fulfilling the necessities 4, 5, and 6, learn my article Introduction to Selenium with Python Three at Linuxhint.com.
Yow will discover many articles on the opposite matters on LinuxHint.com. Make sure you examine them out in the event you want any help.
Setting Up a Venture Listing:
To maintain every part organized, create a brand new mission listing selenium-refresh/ as follows:
$ mkdir -pv selenium-refresh/drivers
Navigate to the selenium-refresh/ mission listing as follows:
Create a Python digital atmosphere within the mission listing as follows:
Activate the digital atmosphere as follows:
$ supply .venv/bin/activate
Set up Selenium Python library utilizing PIP3 as follows:
Obtain and set up all of the required net driver within the drivers/ listing of the mission. I’ve defined the method of downloading and putting in net drivers in my article Introduction to Selenium with Python 3. In the event you want any help, search on LinuxHint.com for that article.
Methodology 1: Utilizing the refresh() Browser Methodology
The primary methodology is the best and the really helpful methodology of the refreshing web page with Selenium.
Create a brand new Python script ex01.py in and kind within the following traces of codes in it.
from selenium import webdriver
from selenium.webdriver.frequent.keys import Keys
from time import sleep
choices = webdriver.ChromeOptions()
choices.headless = True
browser = webdriver.Chrome(executable_path=”./drivers/chromedriver”, choices=choices)
browser.get(“https://www.unixtimestamp.com/”)
timestamp = browser.find_element_by_xpath(“//h3[@class=’text-danger’][1]”)
print(‘Present timestamp: %s’ % (timestamp.textual content.break up(‘ ‘)[0]))
sleep(5)
browser.refresh()
timestamp = browser.find_element_by_xpath(“//h3[@class=’text-danger’][1]”)
print(‘Present timestamp: %s’ % (timestamp.textual content.break up(‘ ‘)[0]))
browser.shut()
When you’re carried out, save the ex01.py Python script.
Line 1 and a pair of imports all of the required Selenium parts.
Line Three imports sleep() perform from time library. I’ll use this to attend a couple of seconds for the webpage to replace in order that we will fetch new information after refreshing the webpage.
Line 5 creates a Chrome Choices object, and line 6 allows headless mode for the Chrome net browser.
Line eight creates a Chrome browser object utilizing the chromedriver binary from the drivers/ listing of the mission.
Line 9 tells the browser to load the web site unixtimestamp.com.
Line 11 finds the ingredient that has the timestamp information from the web page utilizing the XPath selector and shops it within the timestamp variable.
Line 12 parses the timestamp information from the ingredient and prints it on the console.
Line 14 makes use of the sleep() perform to attend for five seconds.
Line 15 refreshes the present web page utilizing the browser.refresh() methodology.
Line 17 and 18 is identical as line 11 and 12. It finds the timestamp ingredient from the web page and prints the up to date timestamp on the console.
Line 20 closes the browser.
Run the Python script ex01.py as follows:
As you possibly can see, the timestamp is printed on the console.
After 5 seconds of printing the primary timestamp, the web page is refreshed, and the up to date timestamp is printed on the console, as you possibly can see within the screenshot under.
Methodology 2: Revisiting the Similar URL
The second methodology of refresh the web page is to revisit the identical URL utilizing the browser.get() methodology.
Create a Python script ex02.py in your mission listing and kind within the following traces of codes in it.
from selenium import webdriver
from selenium.webdriver.frequent.keys import Keys
from time import sleep
choices = webdriver.ChromeOptions()
choices.headless = True
browser = webdriver.Chrome(executable_path=”./drivers/chromedriver”, choices=choices)
browser.get(“https://www.unixtimestamp.com/”)
timestamp = browser.find_element_by_xpath(“//h3[@class=’text-danger’][1]”)
print(‘Present timestamp: %s’ % (timestamp.textual content.break up(‘ ‘)[0]))
sleep(5)
browser.get(browser.current_url)
timestamp = browser.find_element_by_xpath(“//h3[@class=’text-danger’][1]”)
print(‘Present timestamp: %s’ % (timestamp.textual content.break up(‘ ‘)[0]))
browser.shut()
When you’re carried out, save the ex02.py Python script.
Every thing is identical as in ex01.py. The one distinction is in line 15.
Right here, I’m utilizing the browser.get() methodology to go to the present web page URL. The present web page URL could be accessed utilizing the browser.current_url property.
Run the ex02.py Python script as follows:
As you possibly can see, the Pythion script ex02.py prints the identical kind of knowledge as in ex01.py.
Conclusion:
On this article, I’ve proven you 2 strategies of refreshing the present webpage utilizing the Selenium Python library. It’s best to have the ability to do extra fascinating issues with Selenium now.
selenium web driver refresh page,robot framework selenium refresh page,python reload selenium,python selenium refresh page source,how to reload a frame in selenium,selenium js reload page,selenium hard refresh,python selenium refresh page after click