c# - Selenium C# 请尝试/获取帮助
问题描述
我正在尝试在 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");
}
推荐阅读
- angular - 以角度对树节点的子节点进行排序
- php - SQLSTATE[HY000] [2002] 没有这样的文件或目录 (SQL: select * from `users` where `email` = test@test.com limit 1)
- angular - 从 Cloud Firestore 中检索数据
- sql-server - 给一个数字添加一个 0,因为它还没有前导 0
- python - 我需要哪个版本的包不需要升级django
- ios - 根据在 TableViews 自定义单元格中点击的图像执行 segue
- php - 发送没有数据的 ajax 请求,PHP 检查 isset
- python - Python 3 Turtle - 按住 2 个键
- caffe - 为新的测试数据创建 LMDB
- python - pybind11 从 C++ 修改 numpy 数组