What is to be done when you are stuck somewhere in the time of executing your automation script or you do not find a way out to reload the page that is required now?
Selenium comes up with some amazing technologies that help you to solve these sorts of problems easily. Selenium has some very simple commands that can be used to refresh or reload a page. there are some other methods available also to go forward or backward of a page. Today we will get acquainted with some of these features.
Set Up the Environment
So, let us initiate the process. The
WebDriver module needs to be imported from selenium and then create a driver object from it. Next, we need to specify the path of
chromedriver since we will be using the chrome browser to scroll the page. We also need to import the time module. The
maximize_window() method is available to have a better view. Then try to connect to the website using
driver.get() method. Today we will use “ALL MOVIE” website for our purpose.
from selenium import webdriver import time driver = webdriver.Chrome(executable_path = r'G:/chromedriver_win32/chromedriver.exe') driver.maximize_window() driver.get('https://www.allmovie.com/')
Refresh the Page Using refresh()
Selenium Webdriver provides us with a special method called
refresh() which enables us to refresh the page very easily. After loading the “ALL MOVIE” blog page if we want to refresh the page, we can do it with this method. We will set the time difference to 10 seconds so that we can view the refreshing moment of the page clearly. It will take exactly 10 seconds to reload the page. the code goes like this:
The blog will be reloaded exactly after 10 seconds of the initialization of the web page. This method is really useful when we need to refresh a page in the middle of a script or in time of executing the browser refresh instantly.
Backward a Web Page
Let’s click on the “Discover” button of the “ALL MOVIE” web page. If you are not familiar with clicking a button procedure then I suggest you visit the finxter blog and read out the following:
I hope you have already learned about the
click() method of the selenium from the above link. Now we will use the
find_element_by_link_text() method to locate the element of the “Discover” button and then try to click on it with the
click() method. Let’s create a “discover” variable that contains the locating webelement.
time.sleep(3) discover = driver.find_element_by_link_text('Discover') discover.click()
It will take us to the next web page that represents the Discover button.
Now if we want to go back to the earlier page we can do it with the
driver.back() method of the selenium webdriver. The code will follow like this:
After the execution of the code we will come back to the earlier page again.
How to Forward a Web Page
It is also possible with the selenium
webdriver to go to the next page.it can automatically click the forward button to move to the next page. we can achieve this with
driver.forward() method of selenium
webdriver. But before that, we need to use
time.sleep() method again to allow some time for the browser to load properly. The code will follow like this:
We will come back to the desired page after executing the command.
That is all about the methods we are using to refresh a page or to move backward or forward. Hope it was interesting.
To boost your Python skills, join our free email academy:
Yassin Mahmud is a blockchain enthusiast and content creator in the blockchain space. Driven by his passion for distributed technology, he switched to the blockchain sector from the MERN stack development. With over half a decade of experience at present, he is trying to contribute to the blockchain community with his writing. When Yassin is not writing, he can be found walking along the beaches or touring spectacular travel destinations.