Simon
Simon

Reputation: 61

Selenium C# Check if IWebElement exists using PageObject

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);
        }
    }

Upvotes: 3

Views: 4942

Answers (3)

N. Raj
N. Raj

Reputation: 467

To assert presence of an element, with C# and NUnit

Assert.IsTrue(
  btnSubmit.Displayed,
  "Submit button should be displayed"
);

To assert absence of an element, I really like NUnit's Assert.throw syntax.

Assert.Throws<NoSuchElementException>(
  () => btnSubmit.Contains(""),
  "Accessing Submit button should throw NoSuchElementException"
);

Upvotes: 0

dawk24
dawk24

Reputation: 41

You should be able to just do the FindElement in the try based on the id you want to use. If it finds it then it will proceed to returning true, but if it catches the NoSuchElementException then it will return false:

bool CheckIfItExists(string ElementId)
{
    try
    {
        driver.FindElement(By.Id(ElementId));
        return true;
    }
    catch (NoSuchElementException)
    {
        return false;
    }
}

Upvotes: 1

Gaurang Shah
Gaurang Shah

Reputation: 12930

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#.

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;
            }
        });
    }

Upvotes: 1

Related Questions