Is there a way to see if an element exists WITHOUT using a try catch statement?

2,717 views
Skip to first unread message

tommyboy

unread,
Jun 20, 2011, 7:34:16 PM6/20/11
to webdriver
Hello,

I was wondering if there was a way to check for the existence of an
element without having to resort to using a try catch statement using
the C# version of Webdriver (selenium-dotnet-2.0b3).

For example, one a page I run tests on I look for an ad which
sometimes appears. If the ad appears, I press the X button on it to
make it go away before continuing:

// look to see if the ad exists by seeing if we can find its X Button
try
{
IWebElement adElementXButton = driver.FindElement(By.XPath("//
div[@id='blockUI_adunit']"));
adElementXButton.Click();
}
// ad was not found, just continue on with the test case
catch { };

If we try to grab the adElementXButton element outside of a try catch
statement and the element does not exist, Webdriver will automatically
fail the test because an element could not be found. Thus the try
catch statement basically functions the same as an if else statement
where you're doing "else" if an element does not exist. The only
problem is that performing a try catch takes FOREVER and drastically
slows down the execution of the test cases that I'm running.

Is there any way of being able to see whether or not an element exists
without having to worry about the test case failing out if the element
can not be found?

THANK YOU in advance!!!!
-tommy

Luke Inman-Semerau

unread,
Jun 20, 2011, 7:52:19 PM6/20/11
to webd...@googlegroups.com
Short answer is no. But it should be instant and not take forever,
unless somewhere earlier in your code you are setting implicityWait,
then any unfound element will wait that many seconds retrying for the
missing element. If you are and probably some other section of your
code depends on it, set it to 0 before your call and back to what you
need after. (or find what is really needing it and use a WebDriverWait)

-Luke

> --
> You received this message because you are subscribed to the Google
> Groups "webdriver" group.
> To post to this group, send email to webd...@googlegroups.com.
> To unsubscribe from this group, send email to webdriver+...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/webdriver?hl=en
> .
>

dan hirsch

unread,
Jun 21, 2011, 5:22:26 AM6/21/11
to webd...@googlegroups.com
 Another short Answer, yes. Driver.findelements().size() or using JavaScript 

Sent from my iPhone

Luke Inman-Semerau

unread,
Jun 21, 2011, 9:13:49 AM6/21/11
to webd...@googlegroups.com
Dan, that seems counter-intuitive that findelements wouldn't throw an exception if no element is found, but good to know. Do you know if it implicitly waits if it doesn't find any elements or not? (I'll crack open the code later)

-Luke

dan hirsch

unread,
Jun 21, 2011, 10:04:59 AM6/21/11
to webd...@googlegroups.com
It is indeed not elegant but hey we work with what we have. 
It doesn't implicitly wait, if you want you can do it your self using implicitWait

Sent from my iPhone

tommyboy

unread,
Jun 21, 2011, 7:10:29 PM6/21/11
to webdriver
Thank you both Luke and Dan!!

I ended up changing my implicit wait value to a lower number and
indeed it did make my try catch statements take less time. You were
totally correct.

And Dan, I will use Size() in the future to find out the existence of
element. I too assumed that that an exception would be thrown if
nothing was found and it's good to know that it doesn't.
Actually, since I am using the C# version of Webdriver there doesn't
seem to be a Size() function and instead I'm using Count:

int size = driver.FindElements(By.XPath("some xpath")).Count;

.....which I'm assuming is the same thing.

Thanks again!

On Jun 21, 7:04 am, dan hirsch <hirsh....@gmail.com> wrote:
> It is indeed not elegant but hey we work with what we have.
> It doesn't implicitly wait, if you want you can do it your self using
> implicitWait
>
> Sent from my iPhone
>
> On Jun 21, 2011, at 16:14, Luke Inman-Semerau <luke.seme...@gmail.com>
> wrote:
>
> Dan, that seems counter-intuitive that findelements wouldn't throw an
> exception if no element is found, but good to know. Do you know if it
> implicitly waits if it doesn't find any elements or not? (I'll crack open
> the code later)
>
> -Luke
>
> On Jun 21, 2011, at 2:22 AM, dan hirsch <hirsh....@gmail.com> wrote:
>
>  Another short Answer, yes. Driver.findelements().size() or using
> JavaScript
>
> Sent from my iPhone
>
> On Jun 21, 2011, at 2:34, tommyboy < <emailtommy...@gmail.com>
> <http://groups.google.com/group/webdriver?hl=en>http://groups.google.com/group/webdriver?hl=en.
>
>  --
> You received this message because you are subscribed to the Google Groups
> "webdriver" group.
> To post to this group, send email to webd...@googlegroups.com.
> To unsubscribe from this group, send email to
> webdriver+...@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/webdriver?hl=en.

dan hirsch

unread,
Jun 21, 2011, 11:57:00 PM6/21/11
to webd...@googlegroups.com
You probably assume correctly sine it's not related to selenium rather
to the Lang type returned by calling findElements. In java it's a set,
hence the size() method

Sent from my iPhone

Reply all
Reply to author
Forward
0 new messages