0

I am trying to automate google search,normal sendkeys is working ,but when I try to send using keys.F5 or ascii code ,refresh wont work also when try to do location reload it gives error as " The method execute_script(String) is undefined for the type WebDriver "

Tried instead of F5 ,F1 key also but no avail

   ` package com.at.sample;
import org.openqa.selenium.Keys;


import java.lang.Thread;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
// import org.openqa.selenium.Alert;
import java.util.List;





public class Refreshgoogle {
public static void main(String[] args) throws InterruptedException {

    WebDriver driver;
    System.setProperty("webdriver.chrome.driver","c://chromedriver.exe");
     driver= new ChromeDriver();

//Launch the Application Under Test (AUT)
driver.get("http://google.com");


Actions action = new Actions(driver);
WebElement element = driver.findElement(By.name("q"));

element.sendKeys("test data");
//sends normal keybaord strokes

 // approch 1  driver.findElement(By.xpath("//html")).sendKeys(Keys.F5);


// approch 2.1 WebElement element1 = driver.findElement(By.xpath("//*[@id=\"tsf\"]/div[2]/div[1]/div[2]/div[2]"));
//approch 2.2 element1.sendKeys(Keys.F1);


//  approch 3   driver.findElement(By.xpath("//*[@id=\"gsr\"]")).sendKeys(Keys.F5);

  // driver.execute_script("location.reload(true);");


System.out.println(driver.getTitle());
// working driver.navigate().to(driver.getCurrentUrl());


}
}
`

There are 4 approaches First 3 wont refresh pagea when used 4th it shows error as The method execute_script(String) is undefined for the type WebDriver

3 Answers3

0

Please refer below solution

driver.navigate.refresh();

If you want to refresh your page using keys then you can also use Robot class.

    Robot robot = new Robot();  // Robot class throws AWT Exception
    Thread.sleep(2000); // Thread.sleep throws InterruptedException
    robot.keyPress(KeyEvent.VK_CONTROL);  // press Control key down key of 
    robot.keyPress(KeyEvent.VK_F5);
    robot.keyRelease(KeyEvent.VK_CONTROL);  
    robot.keyRelease(KeyEvent.VK_F5);
    Thread.sleep(2000); 

This is selenium related issue more details available here: https://github.com/webdriverio/webdriverio/issues/1344

SeleniumUser
  • 4,065
  • 2
  • 7
  • 30
0

You can refresh in below ways:

1.Using get method and recursive way

    driver.get("https://URL.com");
    driver.get(driver.getCurrentURL());
  1. Using Navigate method and Recursively calling your URL

    driver.get("https://URL.com"); driver.navigate.to(driver.getCurrentURL());

  2. Using one valid webelement and send keys

    driver.get("https://URL.com"); driver. findElement(By.id("username")).sendKeys(Keys.F5);

Hope this help.

Amruta
  • 1,128
  • 1
  • 9
  • 19
0

WebElement(I) sendKeys() will not accept Keys (keyboard keys). This can be handled using Actions class only.

Additionally, if you need to refresh the page, use WebDriver() refresh() or get current URL using getCurrentUrl() of same interface and navigate() using same url as parameter.

Update:

Here is the detailed explanation on each approach: 1) As per 'https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebElement.html#sendKeys-java.lang.CharSequence...-', sendKeys() in WebElement(I) accepts only char sequence (i.e. string.). // approch 1 driver.findElement(By.xpath("//html")) returns a WebElement and this element sendKeys will accept only char sequence. Hence, your approach r=to refresh using Keys.F5 won't work here.

2) // approch 2.1 WebElement element1 = driver.findElement(By.xpath("//*[@id=\"tsf\"]/div[2]/div[1]/div[2]/div[2]")); //approch 2.2 element1.sendKeys(Keys.F1); Same explanation as approach 1.

3) // approch 3 driver.findElement(By.xpath("//*[@id=\"gsr\"]")).sendKeys(Keys.F5); Did the same kind of operation as approach 1 and is explained there.

4) If we need to use javascriptexecutor, first we need to create javascriptexecutor object like below and should call execute_script() using reference variable of that object:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(Script,Arguments);

If you are not created this object, you will get 'execute_script(String) is undefined for the type WebDriver', which is expected.

Hence, the 4 approaches what you tried will not refresh the page. Instead, you can use below options:

1) Actions class sendKeys(): which will accept keyboard keys. 2) using driver.navigate().refresh(); 3) Using javascriptexecutor after creating an object for the same (as explained in approach 4)

Try with this code:

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class TestRefresh {

    public static void main(String[] args) {

        WebDriverManager.chromedriver().setup();

        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.google.com");
`//     case 1:`
        `driver.navigate().to(driver.getCurrentUrl());`
`//     case 2:`
        `((JavascriptExecutor)driver).executeScript("document.location.reload()");`
`//     case 3:`
        `driver.navigate().refresh();`
    }

}
Sreenivasulu
  • 494
  • 2
  • 7
  • refresh using other method working,I mainly want using sendkey for learning purpose which is not working ,sending alphabet also working,but ascii or F5 key its not refreshing – Jobster Vijay Oct 30 '19 at 12:55
  • its nice answer now,so whats trick for using sendkeys and F5 ,which element accept this command? – Jobster Vijay Oct 31 '19 at 06:47
  • If you need to refresh using F5 option, then preferred to go with 'Actions' class sendKeys(). – Sreenivasulu Oct 31 '19 at 09:43
  • is there I use on page? i.e. selecting any other thing than web element – Jobster Vijay Oct 31 '19 at 10:17
  • To refresh the page using Actions class, we do not need WebElement and your code should be like below: Actions actions = new Actions(driver); actions.sendKeys(Keys.F5); – Sreenivasulu Oct 31 '19 at 10:20
  • 1) I meant to say without using action class,plain sendkeys can we try code? 2) I could not find online reference for mentioning F5 key not working for web element you please point me ? – Jobster Vijay Oct 31 '19 at 10:31
  • One point we have to remember that, refresh is used to work on driver level, not on element level. If we try to use sendKeys() of WebElement() will not work as this is element based. Hence, we can go with Actions class sendKeys(). Here the line Actions actions = new Actions(driver); will create an Actions object using that DRIVER. Additionally, if we need to make Actions class to work, we need to call perform() at the end of any Actions statement like 'actions.sendKeys(Keys.F5).perform();'. Then that particular action can be performed on that driver. – Sreenivasulu Nov 06 '19 at 09:23
  • all online tutorial states it works for web element – Jobster Vijay Nov 07 '19 at 10:19
  • I agree the point what you said. Most of the tutorials says same thing. That is what even I wondered how it will be on element level. As per basic logic, refresh is the concept on driver level, but not on element level. You can do one thing, Try in both the ways using WebElement().sendKeys(Keys.F5) and Actions class sendKeys(Keys.F5).perform() and decide which one will work. Thank you :) – Sreenivasulu Nov 09 '19 at 06:20
  • Even I also tried in different ways to simulate refresh using Actions class, but not succeeded. Same issue faced after trying the way they mentioned in this https://stackoverflow.com/questions/12299960/browser-refresh-by-ctrlf5-in-webdriver-using-java also. But we can refresh the page using these three options. 1) driver.navigate().refresh(); 2) String s = driver.getCurrentUrl(); driver.navigate().to(s); . 3) ((JavascriptExecutor)driver).executeScript("document.location.reload()"); You can use any of these two ways to refresh the page. – Sreenivasulu Nov 13 '19 at 11:47
  • issue is ,all function key not working now,I might want to use F1 and f12 mainly and other keys as well – Jobster Vijay Nov 14 '19 at 13:07
  • Yes, All function keys are not working in selenium either by using WebElement sendKeys() or by using Actions class sendKeys(). Not sure why it is – Sreenivasulu Nov 15 '19 at 04:16