Objective: Check if an element/IWebElement exists on the current page using PageObjects.
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. 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);
}
}