I searched on uncountable webpages, and did not get a good answer to my question. I'm on Selenium 2.30 using C#.
I tried
if (browser.FindElement(By.XPath("xpath")).Displayed)
I tried
if (browser.FindElements(By.XPath("xpath")).Count !=0)
And also
IWebElement element = browser.FindElement(By.XPath("xpath"));
if (element.Displayed == true)
They only work when the element exist, but if not, it will pull out the exception. But that's not necessary an exception, I have something in else{} statement to handle it, I don't want the webdriver just stop me at the first point.
What I'm doing right now is
IWebElement element = null;
try
{
element = browser.FindElement(By.XPath("xpath"));
}
catch
{
}
if (element != null)
This way works so far, but I don't think this is the best solution. I appreciate if someone can show me a better way.