2017-07-18 50 views
0

在下面的代碼中,雖然我已經聲明和初始化selenium webdriver中的警報,但在selenium webdriver中會出現以下錯誤。當在selenium webdriver中聲明和啓動警報類型不匹配:無法從警報轉換爲警報錯誤即將到來

Alert action = wait.until(ExpectedConditions.alertIsPresent()); 

在上述警報

類型不匹配:不能從警報轉換通知

快到了,雖然上述警報正確聲明。

+0

第一行應該是wait.until(ExpectedConditions.alertIsPresent());緊接着是Alert action = driver.switchToalert(); –

+0

你使用了什麼導入'Alert'? – Guy

+0

可以添加完整的異常堆棧跟蹤嗎? – Murthi

回答

0

這裏是回答你的問題:

根據你的問題,你所提供的代碼下面一行:

Alert action = wait.until(ExpectedConditions.alertIsPresent()); 

語法上你是非常正確的。

觀察下面的代碼塊快照,alertIsPresent()的調用返回ExpectedCondition<Alert>,我們必須將它分配給Alert類的對象,這是通過您的代碼完成的。

enter image description here

現在來到誤差部分,你看到Type mismatch: cannot convert from Alert to Alert

最可能的原因可能是你的代碼不完整或丟失的進口。要使這行代碼正常工作,您必須具備以下條件:

  1. 在使用實例之前啓動WebDriverWait的實例。
  2. 作出適當的imports
  3. 一個例子:

    import org.openqa.selenium.Alert; 
    import org.openqa.selenium.WebDriver; 
    import org.openqa.selenium.support.ui.ExpectedConditions; 
    import org.openqa.selenium.support.ui.WebDriverWait; 
    //your code 
    WebDriverWait wait8 = new WebDriverWait(driver, 10); 
    Alert action = wait8.until(ExpectedConditions.alertIsPresent()); 
    

通過這個你應該看到過的錯誤。

讓我知道這個答案是否是您的問題。

+0

謝謝@ DebanjanB.It爲我工作.There是該軟件包的一個重要問題。現在代碼完全正常 – jayath

+0

@jayath好消息!您可以通過點擊答案旁邊的勾號來接受答案,就在投票上/下箭頭下面,所以刻度標記變爲綠色?謝謝 – DebanjanB