Selenium 等待特定条件时由WebDriver刷新网页

Selenium 等待特定条件时由WebDriver刷新网页,selenium,refresh,webdriver,selenium-webdriver,Selenium,Refresh,Webdriver,Selenium Webdriver,我正在寻找更优雅的方式在测试期间刷新网页(我使用Selenium2)。 我只是发送F5键,但我想知道驱动程序是否有刷新整个网页的方法 这是我的密码 while(driver.findElements(By.xpath("//*[text() = 'READY']")).size() == 0 ) driver.findElement(By.xpath("//body")).sendKeys(Keys.F5); //element appear after

我正在寻找更优雅的方式在测试期间刷新网页(我使用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();    

对于在手动刷新的页面上查找元素,可能有更好的解决方案。在Python中,有一种方法可以做到这一点:
driver.refresh()
。在Java中可能不一样


或者,您可以
driver.get(“http://foo.bar");,尽管我认为刷新方法应该可以正常工作

在Java或JavaScript中:

driver.navigate().refresh();

这将刷新页面。

页面刷新(F5)

(或)

用python

使用内置方法

driver.refresh()
或者,执行JavaScript

driver.execute_script("location.reload()")

还有一种情况是,您只想刷新页面上的特定iframe,而不想刷新整个页面

您所做的工作如下:

public void refreshIFrameByJavaScriptExecutor(String iFrameId){
        String script= "document.getElementById('" + iFrameId+ "').src = " + "document.getElementById('" + iFrameId+ "').src";
       ((IJavaScriptExecutor)WebDriver).ExecuteScript(script);
}

在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)");

有关实时代码,请参考链接

,这里是略有不同的C版本:

你也可以试试

Driver.Instance.Navigate().Refresh();

需要注意的一点是,driver.navigate().refresh()调用有时似乎是异步的,这意味着它不会等待刷新完成,它只是“启动刷新”,并且在浏览器重新加载页面时不会阻止进一步执行

虽然这似乎只发生在少数情况下,但我们认为最好通过添加手动检查来确保这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();
}
public void reload(){
//记住对当前html根元素的引用
最终WebElement htmlRoot=getDriver().findElement(按.tagName(“html”));
//刷新有时似乎是异步的,所以这有时只是启动刷新,
//但实际上并不是等新鲜的东西吃完
getDriver().navigate().refresh();
//通过检查html根目录是否不再存在,验证页面是否已开始重新加载
最终长启动时间=System.currentTimeMillis();
final long maxLoadTime=TimeUnit.SECONDS.toMillis(getMaximumLoadTime());
布尔值startedReloading=false;
做{
试一试{
startedReloading=!htmlRoot.isDisplayed();
}catch(ElementNotVisibleException | StaleElementReferenceException ex){
startedReloading=true;
}
}而(!startedReloading&&(System.currentTimeMillis()-startTime
一些注意事项:

  • 因为您正在重新加载页面,所以不能只检查给定元素的存在性,因为该元素在重新加载开始之前和完成之后都会存在。因此,有时您可能会得到正确的答案,但页面甚至还没有开始加载
  • 重新加载页面时,检查WebElement.isDisplayed()将引发StaleElementReferenceException。其余的只是覆盖所有的基础
  • getName():获取页面名称的内部方法
  • getMaximumLoadTime():返回允许以秒为单位加载页面的长度的内部方法
  • verify():内部方法确保页面实际加载
同样,在绝大多数情况下,do/while循环只运行一次,因为代码beyond navigate().refresh()在浏览器实际完全重新加载页面之前不会执行,但我们也看到过这样的情况,因为navigate().refresh()在浏览器完成加载之前未阻止。

在PHP中:

$driver->navigate()->refresh();

使用Selenium Webdriver刷新网页的5种不同方式

Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();
没有特殊的额外编码。我刚刚以不同的方式使用了现有的函数来实现它。这是:

  • 使用sendKeys.Keys方法

    driver.get("https://accounts.google.com/SignUp");
    driver.findElement(By.id("firstname-placeholder")).sendKeys(Keys.F5);
    
    driver.get("https://accounts.google.com/SignUp");  
    driver.navigate().refresh();
    
    driver.get("https://accounts.google.com/SignUp");  
    driver.navigate().to(driver.getCurrentUrl());
    
    driver.get("https://accounts.google.com/SignUp");  
    driver.get(driver.getCurrentUrl());
    
    driver.get("https://accounts.google.com/SignUp"); 
    driver.findElement(By.id("firstname-placeholder")).sendKeys("\uE035");
    
  • 使用导航.刷新()方法

    driver.get("https://accounts.google.com/SignUp");
    driver.findElement(By.id("firstname-placeholder")).sendKeys(Keys.F5);
    
    driver.get("https://accounts.google.com/SignUp");  
    driver.navigate().refresh();
    
    driver.get("https://accounts.google.com/SignUp");  
    driver.navigate().to(driver.getCurrentUrl());
    
    driver.get("https://accounts.google.com/SignUp");  
    driver.get(driver.getCurrentUrl());
    
    driver.get("https://accounts.google.com/SignUp"); 
    driver.findElement(By.id("firstname-placeholder")).sendKeys("\uE035");
    
  • 使用导航到()方法

    driver.get("https://accounts.google.com/SignUp");
    driver.findElement(By.id("firstname-placeholder")).sendKeys(Keys.F5);
    
    driver.get("https://accounts.google.com/SignUp");  
    driver.navigate().refresh();
    
    driver.get("https://accounts.google.com/SignUp");  
    driver.navigate().to(driver.getCurrentUrl());
    
    driver.get("https://accounts.google.com/SignUp");  
    driver.get(driver.getCurrentUrl());
    
    driver.get("https://accounts.google.com/SignUp"); 
    driver.findElement(By.id("firstname-placeholder")).sendKeys("\uE035");
    
  • 使用get()方法

    driver.get("https://accounts.google.com/SignUp");
    driver.findElement(By.id("firstname-placeholder")).sendKeys(Keys.F5);
    
    driver.get("https://accounts.google.com/SignUp");  
    driver.navigate().refresh();
    
    driver.get("https://accounts.google.com/SignUp");  
    driver.navigate().to(driver.getCurrentUrl());
    
    driver.get("https://accounts.google.com/SignUp");  
    driver.get(driver.getCurrentUrl());
    
    driver.get("https://accounts.google.com/SignUp"); 
    driver.findElement(By.id("firstname-placeholder")).sendKeys("\uE035");
    
  • 使用sendKeys()方法

    driver.get("https://accounts.google.com/SignUp");
    driver.findElement(By.id("firstname-placeholder")).sendKeys(Keys.F5);
    
    driver.get("https://accounts.google.com/SignUp");  
    driver.navigate().refresh();
    
    driver.get("https://accounts.google.com/SignUp");  
    driver.navigate().to(driver.getCurrentUrl());
    
    driver.get("https://accounts.google.com/SignUp");  
    driver.get(driver.getCurrentUrl());
    
    driver.get("https://accounts.google.com/SignUp"); 
    driver.findElement(By.id("firstname-placeholder")).sendKeys("\uE035");
    

  • 在Java中使用selenium刷新当前页面的另一种方法。

    //first: get the current URL in a String variable
    String currentURL = driver.getCurrentUrl(); 
    //second: call the current URL
    driver.get(currentURL); 
    
    使用此选项将刷新当前页面,就像单击浏览器的地址栏并按enter键一样。

    在中,您可以使用刷新方法,但首先,我们使用导航方法导航到url:

    remDr$navigate("https://...")
    remDr$refresh()
    

    高兴的它帮助了你。或者,如果您使用的是纯selenium RC或WebDriverBackedElenium,您也可以使用:selenium.refresh();但这并不完全等同于按F5键
    driver.navigate().refresh()
    在其请求头中显示“无缓存”,因此无条件重新加载所有内容。而按F5可能会导致“If Modified Since”请求,这可能会得到“304 Not Modified”响应。如果您进行负载测试,您必须记住这一点。@Manpret感谢您的解决方案。值得投票,太好了!有多种方法可以做到这一点。findElement(By.id(“联系我们”)不是一个好方法。更可靠的始终存在元素是好的,例如:“/html/body”。这对努力刷新有用吗?若页面有post数据,firefox会显示post数据对话框http://foo.bar“
    也可以只使用
    driver.get(driver.current\u url)
    带有真值的答案。。。谢谢。我们如何刷新模式页面?使用getCurrentUrl()或get(CurrentUrl)刷新页面吗@RyanskyHeisenbergNo,getCurrentUrl()仅用于提取活动URL。您需要使用driver.get(var_extractedURL)再次调用它。基本上,它通过复制活动URL并使用