2012-04-20 83 views
104

我正在尋找更優雅的方式刷新網頁在測試過程中(我使用Selenium2)。 我只是給F5鍵,但我不知道司機有方法刷新整個網頁 這裏是我的代碼當等待特定條件時刷新網頁由WebDriver

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();  

也許是

回答

207

在Java或JavaScript:

driver.navigate().refresh(); 

這應該刷新頁面。

+0

很高興!它幫助你。或者,如果你使用純硒或者WebDriverBackedSelenium,你也可以使用:selenium.refresh(); – 2012-04-23 18:32:21

+23

雖然它不完全等同於按F5。 ''driver.navigate()。refresh()'在它的請求標題中寫明「no-cache」,並因此無條件地重新加載所有內容。而按F5可能會導致「If-Modified-Since」請求,這可能會得到「304未修改」響應。如果你做負載測試,你必須記住這種差異。 – 2013-02-16 15:49:53

+0

非常感謝你的回答:) Upvote – WEshruth 2015-04-16 07:02:57

35

在Python有一個手動刷新頁面上找到元素一些更好的解決方案這樣做的方法:driver.refresh()。在Java中可能不一樣。

或者,你可以driver.get("http://foo.bar");,雖然我認爲刷新方法應該工作得很好。

+0

使用相同的URL獲取不適用於我的Java。 – kirilloid 2013-04-24 15:41:31

4

替代爲頁面刷新(F5)

driver.navigate().refresh(); 

(或)

Actions actions = new Actions(driver); 
actions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform(); 
21

在蟒

使用內置在方法

driver.refresh() 

,或者執行JavaScript

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

當你想只刷新頁面上特定的iframe和不完整的頁面還有一個情況。

您做如下:

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

找到各種方法來刷新硒的應用: -

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

+0

不錯!多種方式來做到這一點。 findElement(By.id(「聯繫我們」))不是一個好方法。總是呈現更可靠的元素是很好的,例如:'/ html/body'。這是否適用於硬刷新?以及如果頁面發佈數據,firefox被稱爲顯示發佈數據對話框。 – mrtipale 2018-01-30 10:50:56

5

這裏略有不同的C#版本:

driver.Navigate().Refresh(); 
2

您也可以嘗試

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

需要注意的重要一點是,driver.navigate()。刷新()調用有時似乎是異步的,這意味着它不會等待更新完成,它只是「啓動刷新」並且不會在瀏覽器重新加載頁面時阻止進一步的執行。

雖然這似乎只發生在少數情況下,但我們認爲最好通過手動檢查頁面是否確實開始重新加載來確保此項工作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(); 
} 

一些注意事項:

  • 既然你重新加載頁面,你不能只檢查是否存在一個給定的元素,因爲元素將在重載開始之前以及完成之後出現在那裏。所以有時你可能會變得真實,但該網頁甚至沒有開始加載。
  • 當頁面重新加載時,檢查WebElement.isDisplayed()將拋出StaleElementReferenceException。剩下的只是涵蓋所有基地
  • 的getName():內部方法獲取頁面
  • getMaximumLoadTime()的名稱:內部方法,返回頁面多久應該被允許以秒爲
  • 驗證加載( ):內部方法確保頁面實際加載

再次,在絕大多數情況下,do/while循環運行一次,因爲超出navigate()。refresh()的代碼不會執行直到瀏覽器實際上完全重新加載頁面,但是我們已經看到了實際需要幾秒鐘才能通過該循環的情況,因爲navigate()。refresh()沒有bl直到瀏覽器完成加載。

-1

在PHP:

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

5種不同的方式來刷新使用硒的webdriver

沒有特別的額外編碼的網頁。我只是以不同的方式使用現有的功能來實現它。它們是:

  1. 使用sendKeys.Keys方法

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

    driver.get("https://accounts.google.com/SignUp"); 
    driver.navigate().refresh(); 
    
  3. 使用navigate.to()方法

    driver.get("https://accounts.google.com/SignUp"); 
    driver.navigate().to(driver.getCurrentUrl()); 
    
  4. 使用的get()方法

    driver.get("https://accounts.google.com/SignUp"); 
    driver.get(driver.getCurrentUrl()); 
    
  5. 使用的SendKeys()方法

    driver.get("https://accounts.google.com/SignUp"); 
    driver.findElement(By.id("firstname-placeholder")).sendKeys("\uE035"); 
    
0

另一種方式來刷新使用硒在當前頁面Java的。

//first: get the current URL in a String variable 
String currentURL = driver.getCurrentUrl(); 
//second: call the current URL 
driver.get(currentURL); 

使用,這將刷新當前頁面類似點擊瀏覽器的地址欄中並按下Enter鍵。