Condition if element is displayed

81 views
Skip to first unread message

Rafael s

unread,
Feb 10, 2015, 3:20:16 PM2/10/15
to webd...@googlegroups.com
Hi.

Currently I have the following code (Selenium and C#)

   IWebElement masterCard = driver.FindElement(By.XPath("//img[@title='Mastercard']"));
           IWebElement visa = driver.FindElement(By.XPath("//img[@title='Visa']"));

            if (masterCard.Displayed)
           {
               driver.FindElement(By.XPath("//img[@title='Mastercard']")).Click();
           }
           else if (visa.Displayed)
           {
               driver.FindElement(By.XPath("//img[@title='Visa']")).Click();
           }
           else
           {
               Console.WriteLine("Don't have card");
           }

My problem: If the credit card mastercard is not displayed, the condition above show me exception NoSuchElement instead of run else if


any solution?

Cheers

darrell

unread,
Feb 10, 2015, 9:23:29 PM2/10/15
to webd...@googlegroups.com
The FindElement call will throw an exception when it fails to find an element. The FindElements method always returns a value. It will return a list of IWebElement. I haven't written C# in over 2 years. So I cannot remember the exact syntax but the code would be something like:

int numMasterCard = driver.FindElements(By.XPath("//img[@title='Mastercard']")).size();
int numVisaCard = driver.FindElements(By.XPath("//img[@title='Visa']")).size();
if(numMasterCard > 0)
{
    // mastercard exists so you can see if it is displayed and click it
else if(numVisaCard > 0)
{
    // visa card exists so you can see if it is displayed and click it
}
else
{
    Console.WriteLine("Don't have a card");

Øystein

unread,
Feb 11, 2015, 2:35:12 AM2/11/15
to webd...@googlegroups.com

Hi,

 

Hi,

 

There are several solution for this:

 

First: The FindElement throws a NoSuchElementException. You can catch this with a try/catch and continue with your code. Basically wrapping your ifs with try/catch.

 

Second: You can use a single xpath since your goal is to click a card regardless.

 

            try

            {

                driver.FindElement(By.XPath("//img[@title='Mastercard'] | //img[@title='Visa']")).Click();

            }

            catch (NoSuchElementException)

            {

                Console.WriteLine("Don't have a card");

            }

 

Third: You can try the Darrel approach and use the .Count() function for FindElements.

 

PS: It is really of no use to declare a variable and not reuse it in the if sentences. Instead of using this:

 

if (masterCard.Displayed)

           {

               driver.FindElement(By.XPath("//img[@title='Mastercard']")).Click();

           }

 

Use:

 

if (masterCard.Displayed)

           {

               masterCard.Click();

           }

 

//Austin

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
webdriver+...@googlegroups.com.
To post to this group, send email to
webd...@googlegroups.com.
Visit this group at
http://groups.google.com/group/webdriver.
For more options, visit
https://groups.google.com/d/optout.

darrell

unread,
Feb 11, 2015, 9:29:59 AM2/11/15
to webd...@googlegroups.com
One warning about using a try/catch block...

If you use FindElements it will return immediate with a list of IWebElement. If you use FindElement in a try/catch block it will attempt to find the iWebElement until the timeout period is reached. This is typically 30 seconds. So you could be waiting up to 30 seconds if you use a try/catch block.

Darrell

To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+unsubscribe@googlegroups.com.


To post to this group, send email to

Øystein

unread,
Feb 12, 2015, 2:43:15 AM2/12/15
to webd...@googlegroups.com

I would say the warning goes both ways. Using a solution that does not wait can cause issues as well. I would rather wait those 30 seconds and get a correct fail instead of getting errors because you did not wait.

If waiting is a issue for the OP then handle that accordingly by setting timeout or use the WebdriverWait class. I also assumed the OP knew about this and did not feel the need to inform about waiting in my reply.

 

//Austin

To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.


To post to this group, send email to

You received this message because you are subscribed to the Google Groups "webdriver" group.

To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.

Rafael s

unread,
Feb 12, 2015, 10:54:11 AM2/12/15
to webd...@googlegroups.com
Hey all.

I think that work now.

@Darrell. Thanks for light :)

@Austin. I'm trying avoid use many try - catch. I believe that have other solutions for this, like you see below. Thanks :)

For this, the solution is:

            int masterCard = driver.FindElements(By.XPath("//img[@title='Mastercard']")).Count;
            int visa = driver.FindElements(By.XPath("//img[@title='Visa']")).Count;

            if (masterCard > 0)
            {
                Console.WriteLine("Mastercard exist");
                
            }
            else if (visa > 0)
            {
                Console.WriteLine("Visa exist");
            }
            else
            {
                Console.WriteLine("Don' have cards");
            }


ei0haro

unread,
Feb 17, 2015, 2:22:27 AM2/17/15
to webd...@googlegroups.com
I think I would also add a display check. 

FindElement(s) will return hidden elements as well. Not sure how your web page works, but if your cards exists but are hidden, your code will not work (saying that you want to validate that they are displayed). 

            var masterCardElements = driver.FindElements(By.XPath("//img[@title='Mastercard']"));
           
var visaElements = driver.FindElements(By.XPath("//img[@title='Visa']"));


           
if (masterCardElements.Any() && masterCardElements[0].Displayed)
           
{
               
Console.WriteLine("Mastercard exist");
           
}
           
else if (visaElements.Any() && visaElements[0].Displayed)
Reply all
Reply to author
Forward
0 new messages