2016-07-30 49 views
4

此問題與以下類似:
即如何等待進度條消失。
How to wait dynamically until the progress bar to load completely in Selenium Webdriver?在Selenium Webdriver中,ExpectedCondition.elementToBeClickable並未等到進度條消失

煤礦有點不同。這裏當進度條出現時,所有元素都被禁用。所以我使用明確的等待,但仍然得到例外。

場景: 在註冊頁面中,在提供所有細節後,腳本點擊「創建帳戶」按鈕。此時,會出現一個圓形進度條,如果輸入的密碼無效(僅用無效密碼進行驗證),它會持續1或2秒,錯誤消息顯示在註冊頁面的頂部。現在我需要點擊「取消」按鈕並重復該過程。

當進度條出現時,整個頁面將被禁用。只有在進度條消失後,用戶才能繼續。

我已經寫代碼,使用WebDriverWait如下相同:

WebDriverWait myWaitVar = new WebDriverWait(driver,20); 

單擊「創建帳戶」按鈕後,進度條來了,我等到出現取消按鈕。

//Click on Create Account btn: 
driver.findElement(By.id("createAccount")).click(); 

//Wait till "Cancel" button is showing up. At cases, it may take some time. 
myWaitVar.until(ExpectedConditions.elementToBeClickable (By.id("cancelRegister"))); 

//click on Cancel btn: 
driver.findElement(By.id("cancelRegister")).click(); 

當我執行上面的代碼時,每次都在最後一行得到NoSuchElementException

我試過ExpectedCondition.visibilityOfElement(),但這裏也是NoSuchElementException

然後我嘗試使用睡眠方法而不是等待。

Thread.sleep(3000); 

現在腳本工作正常。

我無法理解爲什麼WebDriverWait沒有等到進度條消失?

它已成功解析了elementToBeClickable(),但仍然拋出異常,當我們點擊它時。

回答

3

ExpectedConditions.elementToBeClickable回報元素,如果條件爲真意味着如果元素出現的頁面,並點擊它返回一個元素,沒有必要找這個元素再次,只是省略最後一行如下: -

//Click on Create Account btn: 
driver.findElement(By.id("createAccount")).click(); 

//Wait till "Cancel" button is showing up. At cases, it may take some time. 
WebElement el = myWaitVar.until(ExpectedConditions.elementToBeClickable(By.id("cancelRegister"))); 
el.click(); 

Edited1: - 如果你無法點擊因其他元素收到點擊就可以使用JavascriptExecutor進行點擊如下:

//Click on Create Account btn: 
driver.findElement(By.id("createAccount")).click(); 

//Wait till "Cancel" button is showing up. At cases, it may take some time. 
WebElement el = myWaitVar.until(ExpectedConditions.elementToBeClickable(By.id("cancelRegister"))); 
((JavascriptExecutor)driver).executeScript("arguments[0].click()", el); 

編輯2: - 從提供的例外看來,進度條仍覆蓋在cancelRegister按鈕上。所以,最好的辦法就是等待進度條的隱形第一,然後等待如下cancelRegister按鈕的可見性:

//Click on Create Account btn: 
driver.findElement(By.id("createAccount")).click(); 

//Now wait for invisibility of progress bar first 
myWaitVar.until(ExpectedConditions.invisibilityOfElementLocated(By.id("page_loader"))); 

//Now wait till "Cancel" button is showing up. At cases, it may take some time. 
WebElement el = myWaitVar.until(ExpectedConditions.elementToBeClickable(By.id("cancelRegister"))); 
el.click(); 

希望工程... :)

+0

我試過這個,@Saurabh。它引發以下異常: org.openqa.selenium.WebDriverException:元素在點(575,446)處不可單擊。其他元素將收到點擊:

命令持續時間或超時:83毫秒 –

+0

@AbdulRahman意味着您現在可以找到元素。由於其他元素疊加造成的點擊有問題。因此,在這種情況下,您可以使用'JavascriptExecutor'點擊元素..嘗試編輯答案..希望它有幫助... :) –

+0

@AbdulRahman作爲例外明確說明您的進度條仍然會出現接收點擊的情況,所以最好的方法是先等待進度條的隱身,然後等待取消註冊按鈕的可見性。看到第二個更新的答案.. :) –

2

您可以在那裏等待以確保進度欄消失。

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

WebElement foo = wait.until(new Function<WebDriver, WebElement>() { 
public WebElement apply(WebDriver driver) { 
    return (driver.findElements(By.id("progressbar")).size() == 0); 
} 
}); 
+0

是否適用方法返回WebElement OBJ或布爾?我認爲它的布爾值。感謝您的評論。但我的目標是要知道爲什麼WebDriverWait不起作用? –