how to avoid exception when using FindElement, FindElements and Displayed

7,282 views
Skip to first unread message

winfred zhu

unread,
Jul 9, 2012, 2:49:42 AM7/9/12
to webd...@googlegroups.com
I need to check element is not displayed on current page and I have tried two ways
Assert.IsFalse(driver.FindElement(by).Displayed)
or
Assert.IsFalse(driver.FindElements(by).Any())

but they all throw exceptions 

How can I avoid these to make test passed ??

Krishnan Mahadevan

unread,
Jul 9, 2012, 2:51:56 AM7/9/12
to webd...@googlegroups.com
Asserts are essentially validations. If all you need to check if an element is displayed or not then why dont you use a simple if instead ?

WebElement element = driver.findElement(By.id("foo"));
if (element.isDisplayed()){
//do something
}

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/CfX0rQHGA1EJ.
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.

winfred zhu

unread,
Jul 9, 2012, 4:20:36 AM7/9/12
to webd...@googlegroups.com
because that is my checkpoint in my test, In .Net WebDriver, when you call findElement or Displayed() method and if no element found, they will thrown exception, but in our test, I don't need the exception, I just need findElement return null or WebElement object,  Displayed() return true or false.sometime, we will use these for assertion directly!

在 2012年7月9日星期一UTC+8下午2时51分56秒,Krishnan写道:
To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.

Krishnan Mahadevan

unread,
Jul 9, 2012, 4:22:12 AM7/9/12
to webd...@googlegroups.com
Then shouldn't you be incorporating a call to findElement() within a try..catch block and then choose to do nothing with the exception, but merely return "false" to indicate the element wasn't found/displayed on your webpage ?

Asserts are to be used in tests when you want to run validations.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"


To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/ZkBEhg3YVB0J.

To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.

Fakrudeen Shahul

unread,
Jul 9, 2012, 4:22:24 AM7/9/12
to webd...@googlegroups.com

driver.findElement() will throw NoSuchElementFoundException if the element is not found in the current page.
but driver.findElements() will not throw any excepeitons instead it will return empty list if the element is not found in the current page.

With Regards,
Fakrudeen



winfred zhu

unread,
Jul 9, 2012, 4:29:03 AM7/9/12
to webd...@googlegroups.com
for example, you need to check one element which should not be displayed on page, so when navigating to the page, I just write:

Assert.IsFalse(driver.FindElement(By.by('foo').Displayed).

Am i correct? if yes, when I run the code, exception is thrown out( No reponse from server .....)



在 2012年7月9日星期一UTC+8下午4时22分12秒,Krishnan写道:

Fakrudeen Shahul

unread,
Jul 9, 2012, 4:38:03 AM7/9/12
to webd...@googlegroups.com
Sorry use this code :

      for(WebElement temp : Grid.driver().findElementById("abc")){
           Assert.IsFalse(temp.displayed());
       }

With Regards,
Fakrudeen

Graham

unread,
Jul 9, 2012, 4:57:41 AM7/9/12
to webd...@googlegroups.com
no your'e not right I'm afraid. FindElement throws an exception if an object is not found. Even if you change that method to return null, then the Displayed won't work and I'd guess throw it's own exception. 

You need to create your own wrapper function which will return a boolean.something like:

public boolean isElementDisplay(By locator)
{
try{
WebElement myElement = driver.FindElement(locator);
return myElement.Displayed;
}
catch(Exception e)
{
return false;

winfred zhu

unread,
Jul 9, 2012, 5:28:44 AM7/9/12
to webd...@googlegroups.com
see it!

在 2012年7月9日星期一UTC+8下午4时57分41秒,Graham写道:

darrell

unread,
Jul 9, 2012, 12:07:32 PM7/9/12
to webdriver
From the syntax it looks like you are using C# bindings. I use the
Java bindings so this might not be 100% accurate.

The driver.FindElement(by) will return a WebElement object.
The .Displayed property will tell you if the element is visible to the
user. At this point it is assumed the element exists. Before you see
if the element is visible, you need to see if the element exists. The
Java binding will throw an exception if you attempt to find an element
and it does not exist. To handle this I wrap the findElement call in a
try/catch block. If the element does not exist, the catch body is
executed. In the catch body I would save that the element does not
exist. You need to do something similar in C#. For example, in Java:

boolean visible = false; // assume it is invisible
try {
// try to find the element
WebElement w = driver.FindElement(by);
// if I get to here the element exists
// if it is visible
if(w.Displayed)
visible = true;
} catch(Exception e) {
}
Assert.IsFalse(visible);

This is something like what you need. There are probably small syntax
errors because I'm not a C# programmer.

winfred zhu

unread,
Jul 9, 2012, 11:44:52 PM7/9/12
to webd...@googlegroups.com
Thanks

在 2012年7月10日星期二UTC+8上午12时07分32秒,darrell写道:

Anand

unread,
Jul 25, 2012, 8:12:48 PM7/25/12
to webd...@googlegroups.com
Darrell,
I got your point. but then I have a question as to when should we use findElements() if the findElement() is doing and can do everything we want? 
I haven't yet understood the real use of findElements().
Could you please give one or two examples ?
thanks in advance.  

Smita Sinha

unread,
Jul 26, 2012, 12:37:40 AM7/26/12
to webd...@googlegroups.com
Anand,

Here is a small usecase where I am using findelements.
There is a table whose rows changes according to what we set in a drop box called rows/page.
Like if Rows/Page is set to 20 the the no. of rows in the table will be 20.


These rows in the table have the same parent.
So the xpath //table[@name = 'table']/tr returns the all the rows.
Hence I use
List<WebElement> rows = driver.FindElements(by.xpath("//table[@name = 'table']/tr"));
To get the no. of rows 
rows.size();

Let me know if this helps.

-Smita


--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/29XJXPqK6kwJ.

To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.

Graham

unread,
Jul 26, 2012, 3:22:33 AM7/26/12
to webd...@googlegroups.com
I use it as a pre-check before calling findElement() so I don't need to wrap the call in a try..catch. It also allows me to check that the By is specific enough and not returning a bunch of different elements.

so for example:

    private int getNumberOfMatches() {
        int size = driver.findElements(by).size();

        if (size > 1)
            reporter.log("More than one element being matched - check the description of the object.", MessageType.Warning);

        return size;
    }

    public boolean exists() {
        if (getNumberOfMatches() > 0) {
            WebElement myElement = driver.findElement( by );
            return myElement.Displayed;
        }
        return false;
    }

darrell

unread,
Jul 26, 2012, 10:39:32 AM7/26/12
to webdriver
When you are manually testing an application, is there a time when you
need to identify all the XXX items on a page and do something with
them (or one of them)?

If I'm writing a dynamic script which randomly selects an option from
a select list I might do something like:

By selectBy = By.id("my_select");
// find the SELECT
WebElement select = driver.findElement(selectBy);
// find all the OPTION tags in the SELECT
List<WebElement> options =
select.findElements(By.tagName("option"));
// pick a random number from 0 to n-1 where n is the number of
OPTION elements
Random r = new Random(System.currentMilliseconds());
int n = r.nextInt(options.size());
// get random OPTION
WebElement option = options.get(n);
// select the option
option.click();

Another thing I have done is clicking all links on a page. I'd use
findElements(By.tagName("a")); to find all the tags. Then I'd use an
Iterator to go through all the links (actually a little trickier than
this but I won't get into the details).

Darrell

Mike Riley

unread,
Jul 26, 2012, 12:56:36 PM7/26/12
to webd...@googlegroups.com
On the site I am testing I deal with a lot of tables that can show from 0 to N transactions in them.  So findElements() serves two purposes for me.

1) It gives me a count of how many entries are in the table.

2) It gives me a list of all the elements for a particular column.  I can then do a relative XPath based off that element and address any column in the same row as a particular element in the list.

So (using JDK 7) I create a definition like this and fill it:
List<WebElement> batches = new ArrayList<>();
batches = driver.findElementsByCssSelector(DOTN_Data.ARCH_BATCH_VIEW_BDATE_LINKS);

This should reduce search times, too.  Since any relative searches based off a known element should not have to start from the beginning of the DOM to find a sibling element.

Mike

Iain Rose

unread,
Jul 26, 2012, 3:46:23 PM7/26/12
to webd...@googlegroups.com

Anand

unread,
Jul 26, 2012, 8:33:16 PM7/26/12
to webd...@googlegroups.com
Thank you all. That certainly helped. 


On Sunday, July 8, 2012 11:49:42 PM UTC-7, winfred zhu wrote:
Reply all
Reply to author
Forward
0 new messages