2017-02-14 79 views
2

我正在使用selenium和Java。我希望在頁面上執行任何操作之前等待頁面完全加載。等待頁面在Selenium中完全加載

我已經嘗試了以下方法,但它沒有按預期工作。

public void waitForElementToBeVisible(final WebElement element) { 

    WebDriverWait wait = new WebDriverWait(WebDriverFactory.getWebDriver(), WEBDRIVER_PAUSE_TIME); 

    wait.until(ExpectedConditions.visibilityOf(element)); 
+0

這可能是相關:http://stackoverflow.com/questions/15122864/selenium-wait-until-document-is-ready – Minato

+0

可能重複的[在Selenium等待頁面加載](http://stackoverflow.com/questions/5868439/等待頁面裝入硒) –

+0

該元素是在頁面上加載的最後一件事情嗎?有沒有動態的事情發生在網頁上,使得事情在所有事情似乎都被加載後不斷移動?你是否收到關於不存在的元素的錯誤?或元素陳舊? – mrfreester

回答

0

此方法將一直等到元素可見。 首先這種方法會檢查,元素是否在HTML可用,無論是顯示..它會等到元素將顯示..

public void E_WaitUntilElementDisplay() throws Exception 
{ 
    int i=1; 
    boolean eleche,eleche1 = false; 
    while(i<=1) 
    { 
      try{ 
       eleche = driver.findElements(by.xpath("path")).size()!=0; 
      }catch(InvalidSelectorException ISExcep) 
      { 
       eleche = false; 
      } 
      if(eleche == true) 
      { 

       while(i<=1) 
       { 
        try{ 
         eleche1=driver.findElement(By.xpath("Path")).isDisplayed(); 
        }catch(org.openqa.selenium.NoSuchElementException NSEE){ 
         eleche1=false; 
        } 
        if(eleche1 == true) 
        { 
         i=2; 
         System.out.println("\nElement Displayed."); 
        } 
        else 
        { 
         i=1; 
         Thread.sleep(1500); 
         System.out.println("\nWaiting for element, to display."); 
        } 
       } 
      } 
      else 
      { 
       i=1; 
       Thread.sleep(1500); 
       System.out.println("\nWaiting for element, to display."); 
      } 
    } 
} 
+0

希望..我的回答可以幫助你..或者會讓你接近答案..感謝印度埃迪。 –

0


作爲另一種選擇,也許你可以嘗試這樣的:

if(element.isDisplayed() && element.isEnabled()){ 
    //your code here 
} 

,或者如果你知道要等待多久:

thread.sleep(3000); //where 3000 is time expression in milliseconds, in this case 3 secs