2012-05-04 38 views
2

我想每一個網絡元素的測試腳本之前增加明確的等待硒網絡驅動程序 - 如何使用WAITFOR爲AJAX

我的代碼有

import org.openqa.selenium.support.ui.Wait; 
import org.openqa.selenium.support.ui.WebDriverWait; 
        . 
        . 
WebDriverWait wait = new WebDriverWait(driver, 60); 
wait.until(presenceOfElementLocated(By.id("name"))); 

driver.findElement(By.id("name")).clear(); 
driver.findElement(By.id("name")).sendKeys("Create_title_01"); 

我看到的錯誤:

java.lang.NullPointerException 
at org.openqa.selenium.support.ui.FluentWait$1.apply(FluentWait.java:176) 
at org.openqa.selenium.support.ui.FluentWait$1.apply(FluentWait.java:1) 
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:201) 
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:174) 

感謝

+0

[這](http://seleniumhq.org/docs/04_webdriver_advanced.html#顯式和隱式等待)在單個命令中執行,如果你想的話。 ...和「presenceOfElementLocated(By)」的代碼? –

+0

嗨,thx。沒有爲presenceOfElementLocated(By)編寫任何代碼。 – SUM

回答

5

對於使用presenceOfElementLocated等AjaxCondition方法

你應該使用

org.openqa.selenium.support.ui.ExpectedConditions

類。您的代碼應該是這樣的:

wait.until(ExpectedConditions.presenceOfElementLocated(By.id("name"))); 
0

我會建議使用waitForElementPresent或waitForElementNotPrese NT取決於你的AJAX調用的事件。

不過,如果你還是喜歡在這裏使用wait.until是我做(和它的作品)

wait = new WebDriverWait(wedriver1, TimeSpan.FromSeconds(5)); 
..... 
button.Click(); 
wait.Until(webdriver1 => webdriver2.webelement.GetAttribute("style").Contains("display: block")); 

我給在此之前方法取的狀態屬性變化的一個例子。你可以做這樣的事情..

driver.findElement(By.id( 「名字」))。顯示方式(內wait.until您的病情。

+1

如果您正在檢查元素的存在,則不應該使用'driver.findElement()',因爲如果該元素不在頁面上或不可見,它將拋出一個Exception。你應該使用'driver.findElements()'或在'findElement()'周圍添加一個'try-catch'塊 – Dirk

0

看起來你需要的WebDriverWait wait初始化後以下行:

wait.ignoring(NoSuchElementException.class);