int btnSize = driver.findElements(By.xpath("...")).size();
if (btnSize > 1) {
List<WebElement> b = driver.findElements(By.xpath("..."));
} else if (btnSize == 1){
WebElement b = driver.findElement(By.xpath("..."));
} else {
//How do I throw an Exception (e.g. ElementNotFoundException)
//these variants did not work?
throw ElementNotFoundException;
throw (new ElementNotFoundException);
throw (new ElementNotFoundException("not found"));
throw (new ElementNotFoundException(Exception e));
}
0
A
回答
0
扔someThrowableObject;
所以,在你的個案
throw new ElementNotFoundException("Not found!");
關鍵字新被用來創建一個實例。
2
拋出一個新的異常時,基本上你通過調用它的構造函數來創建一個對象。所以這是throw new ElementNotFoundException("not found");
或throw new ElementNotFoundException(exception)
其中的例外是毫無遺漏的,你的異常對象;)
0
剛上btnSize > 1
使用if/else
和else
會拋出異常爲你,如果btnSize < 1
:
if (btnSize > 1)
{
List<WebElement> b = driver.findElements(By.xpath("..."));
...
}
else
{
WebElement b = driver.findElement(By.xpath("...")); // Might throw an exception
...
}
PS:您還沒有指定你使用在每種情況下,該xpath
,但我得到的感覺它們在所有三種情況下都是相同的xpath
,並且您只想迭代所有按鈕,並且可能根據按鈕的數量返回true/false
。
如果確實如此,那麼你可以簡單地這樣做,而不是:
List<WebElement> buttons = driver.findElements(By.xpath("..."));
for (WebElement button : buttons)
{
button.click(); // or whatever you wanna do with each button...
}
return buttons.size() > 0;
相關問題
- 1. 華廷錯誤ElementNotFoundException
- 2. 如何用Mono.Cecil重新拋出一個ILAsm拋出?
- 3. 如何重新拋出異常
- 4. 更新文件拋出:ZipException拋出
- 5. 拋出或重新拋出異常?
- 6. 更新拋出DataException
- 7. 如何拋出ArrayIndexOutOfBoundsException?
- 8. 如何拋出EmptyResultDataAccessException?
- 9. 拋出新的IllegalOperationException Java
- 10. 拋出新的NotImplementedException - VB
- 11. 拋出新的異常
- 12. 拋出新的CustomException與Marshal.ThrowExceptionForHR
- 13. HtmlUnit中的ElementNotFoundException,雖然元素存在
- 14. Selenium ElementNotFoundException當循環中的元素
- 15. 拋出一個新的異常拋出一箇舊的異常
- 16. '拋出新的異常'需要退出()?
- 17. 如何拋出提升不好的詞法拋出異常
- 18. 拋出拋出的錯誤
- 19. 更新SQL拋出異常
- 20. 而拋出:ZipException新澤西
- 21. C# - ThreadAbortException重新拋出
- 22. 重新拋出異常
- 23. C++重新拋出由
- 24. 更新mongo拋出ConcurrentModificationException?
- 25. 重新拋出錯誤
- 26. 重新拋出從抓
- 27. 異常重新拋出
- 28. 刷新實體拋出NHibernate.UnresolvableObjectException
- 29. 新SPTrustedIdentityTokenIssuer中拋出錯誤
- 30. System.Object.GetType如何拋出NullRefernceException
'拋出新ElementNotFoundException( 「什麼」);' – AntonH