2017-08-28 47 views
0

我使用以下代碼等待加載頁面時。FluentWait不等待elementToBeClickable()方法

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) 
      .withTimeout(40, SECONDS) 
      .pollingEvery(10, SECONDS) 
      .ignoring(NoSuchElementException.class); 

    wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(webelements.labelForInputFileField))); 
    log.info("Page loaded!"); 

它不工作,我得到以下錯誤:

java.lang.NullPointerException at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:787) at org.openqa.selenium.support.ui.FluentWait.(FluentWait.java:96) at org.openqa.selenium.support.ui.FluentWait.(FluentWait.java:87)

我試着用presenceOfElementLocated()方法太多,但同樣的錯誤。請求的頁面已加載,我在瀏覽器中直觀地看到它。

+0

IMO兼容,你已經混了'FluentWait'和'WebDriverWait'。您是否可以更新我們您正在嘗試自動化的確切手動步驟以及相關的HTML? – DebanjanB

+0

有一個文件選擇器,在頁面加載後應該點擊它。看來,它不會等到頁面加載。 – plaidshirt

回答

1

試試下面FluentWait代碼: -

WebElement waitsss(WebDriver driver, By elementIdentifier){ 
    Wait<WebDriver> wait = 
       new FluentWait<WebDriver>(driver).withTimeout(60, TimeUnit.SECONDS) 
               .pollingEvery(1, TimeUnit.SECONDS) 
               .ignoring(NoSuchElementException.class); 

     return wait.until(new Function<WebDriver, WebElement>() 
       { 
        public WebElement apply(WebDriver driver) { 
          return driver.findElement(elementIdentifier); 
        } 
       }); 
} 

如果還是不行。檢查你的XPath。這可能是你的XPATH無效,所以FluentWait投擲期望

另一件事是FluentWaitExplicit wait是兩種不同類型的等待。你不能與其他

混合顯式的等待使用下面的代碼:

WebDriverWait wait = new WebDriverWait(ad, 100); 
wait.until(ExpectedConditions.elementToBeClickable(By.id("gst")).sendKeys(username); 

低於參考: -

http://toolsqa.com/selenium-webdriver/implicit-explicit-n-fluent-wait/

或使用JavascriptExecutor

WebElement Searchelement=driver.findElement("Your locator"); 
JavascriptExecutor executor = (JavascriptExecutor)driver; 
executor.executeScript("arguments[0].click();", Searchelement); 

希望它將幫助你:)

+0

不適用於我,具有相同的效果。 Xpath應該沒問題,因爲我之前在2.53.1版本中使用了'WebDriverWait'。我只是試着將我的測試用例更新到3.5。 – plaidshirt

+0

我已經添加了JavascriptExecutor的代碼來點擊元素..試試吧 –

+0

與此代碼同樣的效果,NullPointerException。 – plaidshirt

1
  1. java.lang.NullPointerException - 此例外通常表明smthn爲空,請確保在執行此方法時設置變量驅動程序和webelements。
  2. 確保您使用的不是番石榴的版本,是不是與您當前的框架
+0

請檢查我的問題。 – plaidshirt