使用sendkeys、Selenium JAVA刷新不起作用
我正在尝试自动谷歌搜索,正常的sendkeys是有效的,但当我尝试使用keys.F5或ascii代码发送时,刷新将无效 另外,当尝试执行位置重新加载时,会出现错误,因为“类型WebDriver的方法执行脚本(字符串)未定义 " 尝试使用F1键代替F5键,但无效使用sendkeys、Selenium JAVA刷新不起作用,selenium,selenium-webdriver,selenium-chromedriver,Selenium,Selenium Webdriver,Selenium Chromedriver,我正在尝试自动谷歌搜索,正常的sendkeys是有效的,但当我尝试使用keys.F5或ascii代码发送时,刷新将无效 另外,当尝试执行位置重新加载时,会出现错误,因为“类型WebDriver的方法执行脚本(字符串)未定义 " 尝试使用F1键代替F5键,但无效 ` package com.at.sample; import org.openqa.selenium.Keys; import java.lang.Thread; import org.openqa.selenium.By
` 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());
}
}
`
有4种方法
前3个不会刷新页面A
当第四次使用时,它显示错误,因为类型WebDriver的方法execute_脚本(字符串)未定义请参阅下面的解决方案
driver.navigate.refresh();
如果您想使用键刷新页面,那么还可以使用Robot类
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);
这是与硒相关的问题更多详细信息,请参见:您可以通过以下方式刷新: 1.使用get方法和递归方式
driver.get("https://URL.com");
driver.get(driver.getCurrentURL());
driver.get(“https://URL.com");
driver.navigate.to(driver.getCurrentURL())代码>
driver.get(“https://URL.com");
司机。findElement(By.id(“用户名”)).sendKeys(key.F5)代码>
js.executeScript(脚本、参数) 如果未创建此对象,您将得到“类型WebDriver的执行脚本(字符串)未定义”,这是预期的 因此,您尝试的4种方法不会刷新页面。 相反,您可以使用以下选项: 1) 操作类sendKeys():将接受键盘键。 2) 使用driver.navigate().refresh(); 3) 为javascriptexecutor创建对象后使用javascriptexecutor(如方法4所述) 请尝试使用以下代码:
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();`
}
}
尝试使用
driver.navigate.refresh()
是否有任何特定的原因要使用发送密钥
?您将密钥发送到元素,而不是浏览器或操作系统system@Pratik我只是在学习阶段,想尝试所有方法我说我想使用SendKeys这是我在标题中提到的使用其他方法工作的问题,我主要想使用sendkey进行学习,这不起作用,发送字母表也起作用,但ascii或F5键现在无法刷新它的好答案,那么使用sendkey和F5有什么诀窍,哪个元素接受此命令?如果需要使用F5选项刷新,则首选使用“Actions”类sendkeys().第页有我用的吗?i、 e.选择web元素以外的任何其他内容要使用Actions类刷新页面,我们不需要WebElement,您的代码应如下所示:Actions Actions=新操作(驱动程序);动作。发送键(键。F5);