我正在寻找更优雅的方式来在测试期间刷新网页(我使用Selenium2)。 我只是发送F5键,但我想知道驱动程序是否有刷新整个网页的方法 这是我的代码
while(driver.findElements(By.xpath("//*[text() = 'READY']")).size() == 0 )
driver.findElement(By.xpath("//body")).sendKeys(Keys.F5);
//element appear after text READY is presented
driver.findElement(By.cssSelector("div.column a")).click();
在手动刷新页面上查找元素可能是更好的解决方案
答案 0 :(得分:259)
在Java或JavaScript中:
driver.navigate().refresh();
这应该刷新页面。
答案 1 :(得分:57)
在Python中有一种方法:driver.refresh()
。它在Java中可能不一样。
或者,您可以driver.get("http://foo.bar");
,但我认为刷新方法应该可以正常工作。
答案 2 :(得分:37)
在python中
使用内置方法
driver.refresh()
或执行JavaScript
driver.execute_script("location.reload()")
答案 3 :(得分:16)
使用Selenium Webdriver
刷新网页的5种不同方法没有特殊的额外编码。我刚刚以不同的方式使用现有函数来使其工作。他们在这里:
使用 sendKeys.Keys 方法
driver.get("https://accounts.google.com/SignUp");
driver.findElement(By.id("firstname-placeholder")).sendKeys(Keys.F5);
使用 navigate.refresh()方法
driver.get("https://accounts.google.com/SignUp");
driver.navigate().refresh();
使用 navigate.to()方法
driver.get("https://accounts.google.com/SignUp");
driver.navigate().to(driver.getCurrentUrl());
使用 get()方法
driver.get("https://accounts.google.com/SignUp");
driver.get(driver.getCurrentUrl());
使用 sendKeys()方法
driver.get("https://accounts.google.com/SignUp");
driver.findElement(By.id("firstname-placeholder")).sendKeys("\uE035");
答案 4 :(得分:14)
找到了在selenium中刷新应用程序的各种方法: -
1.driver.navigate().refresh();
2.driver.get(driver.getCurrentUrl());
3.driver.navigate().to(driver.getCurrentUrl());
4.driver.findElement(By.id("Contact-us")).sendKeys(Keys.F5);
5.driver.executeScript("history.go(0)");
有关实时代码,请参阅链接http://www.ufthelp.com/2014/11/Methods-Browser-Refresh-Selenium.html
答案 5 :(得分:9)
这是稍微不同的C#版本:
driver.Navigate().Refresh();
答案 6 :(得分:4)
替代页面刷新(F5)
driver.navigate().refresh();
<强>(或)强>
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();
答案 7 :(得分:3)
需要注意的一件重要事情是,driver.navigate()。refresh()调用有时似乎是异步的,这意味着它不会等待刷新完成,它只会启动刷新&#34; ;并且在浏览器重新加载页面时不会阻止进一步执行。
虽然这种情况似乎只发生在少数情况下,但我们认为通过添加手动检查页面是否真正开始重新加载来确保100%工作更好。
这是我在基页对象类中为此编写的代码:
public void reload() {
// remember reference to current html root element
final WebElement htmlRoot = getDriver().findElement(By.tagName("html"));
// the refresh seems to sometimes be asynchronous, so this sometimes just kicks off the refresh,
// but doesn't actually wait for the fresh to finish
getDriver().navigate().refresh();
// verify page started reloading by checking that the html root is not present anymore
final long startTime = System.currentTimeMillis();
final long maxLoadTime = TimeUnit.SECONDS.toMillis(getMaximumLoadTime());
boolean startedReloading = false;
do {
try {
startedReloading = !htmlRoot.isDisplayed();
} catch (ElementNotVisibleException | StaleElementReferenceException ex) {
startedReloading = true;
}
} while (!startedReloading && (System.currentTimeMillis() - startTime < maxLoadTime));
if (!startedReloading) {
throw new IllegalStateException("Page " + getName() + " did not start reloading in " + maxLoadTime + "ms");
}
// verify page finished reloading
verify();
}
一些注意事项:
同样,在绝大多数情况下,do / while循环运行一次,因为除了navigate()。refresh()之外的代码没有被执行,直到浏览器实际完全重新加载页面,但我们& #39;已经看到通过该循环实际需要几秒钟的情况,因为navigate()。refresh()没有阻塞,直到浏览器完成加载。
答案 8 :(得分:1)
在R中,您可以使用refresh方法,但是首先,我们使用Navigation方法导航到url:
http://localhost:9200/test-auditing-2019-10/_search?from=32&size=32&pretty=true
答案 9 :(得分:0)
还有一种情况是,您只想刷新页面上的特定iframe,而不是整页。
您这样做:
public void refreshIFrameByJavaScriptExecutor(String iFrameId){
String script= "document.getElementById('" + iFrameId+ "').src = " + "document.getElementById('" + iFrameId+ "').src";
((IJavaScriptExecutor)WebDriver).ExecuteScript(script);
}
答案 10 :(得分:0)
在PHP中:
$driver->navigate()->refresh();
答案 11 :(得分:0)
使用Java中的selenium刷新当前页面的另一种方法。
//first: get the current URL in a String variable
String currentURL = driver.getCurrentUrl();
//second: call the current URL
driver.get(currentURL);
使用此按钮将刷新当前页面,例如单击浏览器的地址栏并按Enter键。