如何检查c#硒驱动程序中是否存在Element [英] How to check if Element exists in c# Selenium drivers
问题描述
所以我有一个使用Firefox硒webdriers的c#winform。
So I have this c# winform using Firefox selenium webdriers.
基本上我需要它来检查元素是否存在以及是否不单击其他元素。如果有视频并且在观看之后成为W_VIEWED
Basically i need it to check if an element exists and if it doesn't click on a different one. If there is a video and after it is watched it become W_VIEWED
这是我到目前为止所得到的
Here is what i got so far
driver.FindElement(By.XPath("//div[@class='video']/a")).Click();
else {
driver.FindElement(By.XPath("//div[@class='W_VIEWED']/a")).Click();
}
错误3仅分配,调用,递增,递减,等待和新对象表达式可以用作语句242
Error 3 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement 242
c#硒的新种类。谢谢你的帮助。
Kind of new to c# selenium. Thank you for help.
推荐答案
您可以检查元素出口或不使用
You can check element exits or not using
bool isElementDisplayed = driver.findElement(By.xpath("element")).isDisplayed()
请记住,如果 findElement
找不到元素,则抛出异常,因此您需要正确处理它。
Remember, findElement
throws exception if it doesn't find element, so you need to properly handle it.
在我的一个应用程序中,我通过检查单独函数中的元素来处理异常,
In one of my application I handled exception by checking element in separate function,
private bool IsElementPresent(By by)
{
try
{
driver.FindElement(by);
return true;
}
catch (NoSuchElementException)
{
return false;
}
}
调用函数,
if (IsElementPresent(By.Id("element name")))
{
//do if exists
}
else
{
//do if does not exists
}
这篇关于如何检查c#硒驱动程序中是否存在Element的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!