gpt4 book ai didi

c# - Selenium C# try/catch 帮助请

转载 作者:太空宇宙 更新时间:2023-11-03 12:09:03 32 4
gpt4 key购买 nike

我正在尝试在 Selenium C# 中编写一个 try/catch,如果 Web 元素不存在,则捕获 NoSuchElementException,如果该元素存在,则抛出自定义异常。编码非常绿色,因此将不胜感激所有帮助。谢谢!

try
{
IWebElement spIcon = driver.FindElement(By.CssSelector("#gridview-1080-record-2658335 > td.x-grid-cell.x-grid-td.x-grid-cell-headerId-propertiesColInv.wrappable.icon-spacer.x-unselectable.wrappable.icon-spacer > div > i"));
}
catch (NoSuchElementException spIcoNotDisplayed)
{
//if spIcon is NOT present;
//then continue;
//else throw custom exception
}

最佳答案

var elementPresent = true;
try {
IWebElement spIcon = driver.FindElement(By.CssSelector("#gridview-1080-record-2658335 > td.x-grid-cell.x-grid-td.x-grid-cell-headerId-propertiesColInv.wrappable.icon-spacer.x-unselectable.wrappable.icon-spacer > div > i"));
}
catch (NoSuchElementException spIconNotDisplayed) {
elementPresent = false;
}

if (elementPresent) {
throw new ElementPresentException("The spIcon was found");
}

关于c# - Selenium C# try/catch 帮助请,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53378102/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com