Riaz Ladhani
Riaz Ladhani

Reputation: 4072

WebDriver i am trying to use isElementPresent but error shows not exist in the current context

I am new to WebDriver and writing this code in C# Visual Studio (code snippet below) I am verifying if a text field is present on the home page using IsElementPresent. I get the error The name IsElementPresent does not exist in the current context. What am i doing wrong?

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace Homepage_check2
{
    [TestFixture]
    public class Driver
    {
        IWebDriver driver;

        [SetUp]
        public void Setup()
        {
            // Create a new instance of the Firefox driver
            driver = new FirefoxDriver();
        }

        [TearDown]
        public void Teardown()
        {
            driver.Quit();
        }

        [Test]
        public void homepage()
        {
            //Navigate to the site
            driver.Navigate().GoToUrl("http://www.milkround.com");

           Assert.IsTrue(IsElementPresent(By.Id("ctl00_uxToolbar_uxQueryTextBoxToolbar")));

        }
        catch
        {
            //verificationErrors.Append(e.Message);
        }

        }
    }
}

Upvotes: 0

Views: 4497

Answers (1)

Robbie Wareham
Robbie Wareham

Reputation: 3438

Where does "IsElementPresent" come from? I have never seen that used in WebDriver.

In WebDriver you need to do wrap a try catch around the findElement method.

e.g

Boolean elementDisplayed;

try {

WebElement element = driver.findElement(By.Id("ctl00_uxToolbar_uxQueryTextBoxToolbar"));
elementDisplayed = element.displayed; 

}
catch (NoSuchElementException e) {
elementDisplayed = false;
}

Obviously you can wrap this in a helper method of some kind of perhaps add it to the WebDriver classes.

I'll leave that to you, but this is the general idea

Upvotes: 1

Related Questions