2017-03-16 61 views
0

我想在無頭瀏覽器中進行一些測試,以測試填充表單。PhantomJS和Selenium - 在搜索元素時嘗試使用等待時遇到問題

我使用,我是從下面的示例所示的代碼:

github link to example of using phantomjs

我的代碼:

WebDriverWait wait = new WebDriverWait(driver, 30); 
driver.get("<URL HERE, left out for privacy on stack overflow!>"); 
By amount = By.id("amountField"); 
wait.until(ExpectedConditions.presenceOfElementLocated(amount)); 

錯誤的IntelliJ:

until(java.util.function<? super org.openqa.selenium.WebDriver, V) in FluentWait cannot be applied to : 

(org.openqa.selenium.support.ui.ExpectedCondition<org.openqa.selenium.WebElement>) 

我不是我真的很確定我在哪裏錯了 - 但我花了兩個小時嘗試解決這個問題。

元素我要找頁:

<input name="amount0" id="amountField" value="" class="amount form-control" type="number" pattern="\d+(\.\d*)?" maxlength="10" placeholder="Amount"> 
+0

你能對你的問題有點特別嗎? 1.你的目標是什麼? 2.你想達到什麼目的? 3.爲什麼你需要一個明確的等待? 4.它是一個隱藏的元素? 5.它是如何出現的? 6.提供HTML DOM。 – DebanjanB

+0

請將上面的內容作爲流利等待而不是顯式等待。 – DebanjanB

+0

試圖測試填寫表單,我需要流利的等待,因爲phantomjs似乎試圖在加載之前獲取元素。元素不隱藏。它顯示爲一個測試框。不幸的是,我沒有完整的DOM頁面,但是現在將該元素粘貼到主要問題中。 – MickeyThreeSheds

回答

1

看來,這個問題是不是與PhantomJS。你看到的錯誤告訴了這一切。

你的代碼似乎考慮WebDriverWait wait作爲流利的等待在那裏,你已經出口import org.openqa.selenium.support.ui.ExpectedConditions;這是造成錯誤。

在文檔以及,筆者已經使用顯式等待進口import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.WebDriverWait;

我建議你試試這個方法:

  1. 讓網址,以獲取第一次加載。
  2. 定義顯式等待。
  3. 直到定義ExpectedConditions。
  4. 獲取元素的狀態。
  5. Sysout狀態。
  6. 如果失敗,請增加顯式等待的時間以完成ExpectedConditions。

你可以看看這個修改後的代碼:

driver.get("<URL HERE, left out for privacy on stack overflow!>"); 
WebDriverWait wait = new WebDriverWait(driver, 10); 
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("amountField"))); 
     boolean status=element.isDisplayed(); 

     if (status) 
     { 
      System.out.println("Element is displayed"); 
     } 
     else 
     { 
      System.out.println("Element is not displayed"); 
     } 

讓我知道如果這能幫助你。

+0

你有機會看看這個解決方案嗎? – DebanjanB

+0

@MickeyThreeSheds如果本答案滿足您的問題,請您接受答案以結束討論? – DebanjanB

+0

對不起 - 花了我一段時間回到你身邊 - 我的兄弟正在死去,這有時會讓我離開我的日常活動。 – MickeyThreeSheds

相關問題