Tuesday 13 October 2015

How to refresh or reload a page in selenium webdriver

Sometimes we need to refresh the web page to ensure that all the elements are loaded. In some of the cases we see that for the first attempt all elements are not loaded but if we load the page for the second time we see that all the components are loaded. Some of the cases we need to load pages more than one times. In that case we use some conditional loops until the components are loaded.
We can refresh the page in many ways. Here we will discuss some of the ways.

Using refresh() method:
Selenium webdriver has a method called refresh(). This is widely used command. It refreshes the current page after executing. 
In Java,
driver.navigate().refresh();
Using sendkeys() method:
sendkeys() method is used like we do page refresh pressing F5 key through our keyboard. This manual task is executed by code using the sendkeys() command over an element.
The command will be like
driver.findElement(By.id(locator)).sendKeys(F5 key);
In Java,
driver.findElement(By.id("gbqfq")).sendKeys(Keys.F5);
Using navigate().to() method:
Actually browsing the same URL using navigate().to() function. We call getCurrentUrl() function to get the current URL of the page.
In Java,
driver.navigate().to(driver.getCurrentUrl());  
Using get() method:
If we know the URL then we can load the same URL again to reload the page. In Java,
driver.get("https://www.google.com.bd/");
We can also use getCurrentUrl() function to know the current URL of the page. The command would be, 
driver.get(driver.getCurrentUrl());
Using sendkeys() method with ASCII code:
We can use ASCII code as argument on sendkeys() method that is equivalent to F5 key command of the keyboard.
driver.findElement(By.id("gbqfq")).sendKeys("\uE035");
Here \uE035 is the ASCII code of F5 key.
 
Using executeScript() method:
Using this command we can execute any JavaScript for our need. If we execute location.reload() JavaScript function then current page will be reloaded which meets our purpose. 
The command is,
driver.executeScript("location.reload()"); 
There are many other ways but these are the generally used ways. I think this will help us.

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. wow ! nice post Indian Cyber Army gives the best in the Cyber crime investigation. We have put our foot one-step forward launching an effective Summer Internship program.The Summer Internship 2018 registrations are open for 2nd batch starting from 2 july 2018. Our standard services of Cyber Crime Investigation are class apart owing to years of proficiency. We provide Advanced Cyber Security, Ethical Hacking, Digital Forensics, etc. trainings to keep India totally secure from the Cyber Offenders.

    ReplyDelete
  3. Appreciable! The training is conducted by subject specialist corporate professionals with wide experience in managing real-time ethical hacking training / cyber security projects. Indian Cyber Army implements a blend of academic learning and practical sessions to give the candidate optimum exposure.

    ReplyDelete