1

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.

Jialu Zhu
  • 69
  • 2
  • 9

1 Answers1

1

The way you are doing it is acceptable, but at times you will be trying to run this after performing a prior action (eg. navigating to a page) and it is usually best to give a timeout value and utilise the following WebDriverWait method:

WebDriverWait _wait = new WebDriverWait(_driver, new TimeSpan(0, 0, timeout));
element = _wait.Until(x => x.FindElement(By.XPath(searchAttribute.attributeValue)));

This allows you to wait until the element exists on the page up to the timeout value (I use 5 seconds on the application I test). However, simple you can just use your code of if(element == null) then it is was not found. If you use the WebDriverWait, you will have to catch the exception if you do not want it to throw after the timeout.

Nashibukasan
  • 2,028
  • 23
  • 37
  • 1
    Also have a look at the list of pre-canned expected conditions available to you if using an explicit wait: https://code.google.com/p/selenium/source/browse/dotnet/src/WebDriver.Support/UI/ExpectedConditions.cs – Ardesco Apr 17 '13 at 05:33
  • Thanks Nashibukasan. Maybe here is a stupid question, but what are the differences between WebDriverWait and ImplicitlyWait? – Jialu Zhu Apr 17 '13 at 16:06
  • Another question is, why I can't use if (browser.FindElements(By.XPath("xpath")).Count !=0) – Jialu Zhu Apr 17 '13 at 17:06
  • `ImplicitlyWait` is a timeout set to be used for all following `WebElement` searches. `WebDriverWait` can be passed varying timeout values and can be used for different `ExpectedConditions`. I believe you could use your `Count != 0` code, however you would still benefit from using one of the Waits otherwise you will find it returning a Count of 0 very quickly if the element has not yet loaded. – Nashibukasan Apr 17 '13 at 22:04
  • I got an exception something like "An unhandled exception of type 'OpenQA.Selenium.WebDriverException' occured in WebDriver.dll". Additional information: The HTTP request to the remote WebDriver server for URL 'http://localhost:7055/someurl' timed out after 60 seconds when using .Count != 0 – Jialu Zhu Apr 19 '13 at 16:50
  • Maybe post a new question involving the full details of your exception. However, unfortunately, it sounds a lot like [this issue](http://jimevansmusic.blogspot.com.au/2012/11/net-bindings-whaddaymean-no-response.html). – Nashibukasan Apr 21 '13 at 04:20