![](/img/trans.png)
[英]C#/WPF Error: An Unhandled Exception of Type 'System.StackOverflow Exception' Occured (Tic-Tac-Toe)
[英]C# system.stackoverflow exception
我在if / else語句上收到stackoverflow異常。
代碼示例:
if (driver.FindElements(By.XPath("//*[@id='modal']/div/div/div/p[contains(text(), 'Hello World')]")).Count != 0)
{
Console.WriteLine("Hello World");
}
else
{
RunOtherFunction();
}
突出顯示的錯誤代碼是-
if (driver.FindElements(By.XPath("//*[@id='modal']/div/div/div/p[contains(text(), 'Hello World')]")).Count != 0)
如此明顯地由於找不到指定的元素而引發了異常,但是我用else語句覆蓋了它?
所以我不明白為什么會引發異常,因為如果未找到Element,則應執行“ RunOtherFunction();”,而是引發異常?
突出顯示的行是正確的。 它在第一行引發異常,並且由於您沒有try catch塊,因此不會進入其他行。
基於Java觀點的答案:
我用類似的用例做了一個小測試:
https://www.google.com/
if-else {}
循環中使用findElements()
在頁面上查找id
為Rs_Sele的元素,並使用!=
運算符與0比較 這是示例代碼:
WebDriver driver = new ChromeDriver(options); driver.get("https://www.google.com/"); if(driver.findElements(By.id("Rs_Sele")).size() !=0) System.out.println("Atleast one element was found"); else System.out.println("No element was found");
控制台輸出:
No element was found
findElements()
方法返回一個空列表 ,比較之后,控件轉到else {}
塊並打印未找到任何元素 。 driver.FindElements(By.XPath("//*[@id='modal']/div/div/div/p[contains(text(), 'Hello World')]"))
返回一個空列表 else {}
塊,然后調用RunOtherFunction()
函數。 如果您正在觀察stackoverflow異常,則是由於函數RunOtherFunction();所致。
您可以在中找到關於StackOverflowException的類似討論:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.