[英]Selenium C# Check if IWebElement exists using PageObject
Objective: Check if an element/IWebElement exists on the current page using PageObjects.目标:使用 PageObjects 检查当前页面上是否存在元素/IWebElement。
I am aware of the fact that you can use something like:我知道您可以使用以下内容:
IWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3));
IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Id("foo")));
But as i use PageObjects i dont want to use the id/xpath etc again.但是当我使用 PageObjects 时,我不想再次使用 id/xpath 等。 I am currently using the method beneath to check whether an element exists.我目前正在使用下面的方法来检查元素是否存在。 But to do this fast i first set the Implicit wait and after i reset it to the default value.但是为了快速做到这一点,我首先设置了隐式等待,然后将其重置为默认值。 It works quite well, but it feels dirty.它工作得很好,但感觉很脏。
I found some other older post .我发现了一些其他较旧的帖子。 But this didn't provide any solution to me yet.但这还没有为我提供任何解决方案。 Hope you can help!希望你能帮上忙!
PageObject:页面对象:
[FindsBy(How = How.Id, Using = "errorMessage")]
public IWebElement btnSubmit { get; set; }
Calling the method:调用方法:
CheckElementExists(errorMessage))
The method;方法;
public bool CheckElementExists(IWebElement pageObject)
{
Browser.getDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(100);
try
{
return pageObject.Equals(pageObject);
}
catch (NoSuchElementException)
{
return false;
}
finally
{
Browser.getDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
}
}
You should be able to just do the FindElement
in the try based on the id
you want to use.您应该能够根据要使用的id
在 try 中执行FindElement
。 If it finds it then it will proceed to returning true
, but if it catches the NoSuchElementException
then it will return false
:如果找到它,它将继续返回true
,但如果它捕获NoSuchElementException
则它将返回false
:
bool CheckIfItExists(string ElementId)
{
try
{
driver.FindElement(By.Id(ElementId));
return true;
}
catch (NoSuchElementException)
{
return false;
}
}
I don't have experince with C# but I think it's very similar to JAVA and so you can conver the following JAVA code in C#.我没有使用 C# 的经验,但我认为它与 JAVA 非常相似,因此您可以在 C# 中转换以下 JAVA 代码。
if want to write function just to check if element exist, this is how you can do it.如果只想编写函数来检查元素是否存在,这就是你可以做到的。
public boolean isElementExisit(WebElement element){
try{
element.getTagName();
return true;
}catch (NoSuchElementException){
return false;
}
}
if you want to write something which requires to wait, then you can use fluent wait.如果你想写一些需要等待的东西,那么你可以使用流畅的等待。
public void waitUntilElementIsPresent(WebElement element, int timeout){
Wait wait = new FluentWait<WebDriver>(driver)
.withTimeout(timeout, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
wait.until(new Function() {
@Override
public Boolean apply(Object o) {
element.getTagName();
return true;
}
});
}
To assert presence of an element, with C# and NUnit使用 C# 和NUnit断言元素的存在
Assert.IsTrue(
btnSubmit.Displayed,
"Submit button should be displayed"
);
To assert absence of an element , I really like NUnit's Assert.throw syntax.为了断言没有元素,我真的很喜欢NUnit 的 Assert.throw语法。
Assert.Throws<NoSuchElementException>(
() => btnSubmit.Contains(""),
"Accessing Submit button should throw NoSuchElementException"
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.