0

I am trying to find an element in the else block, if the condition in the if block doesn't work.

  try
    {
       if(Driver.driver.findElement(By.xpath("//div[@id='Tpay_success']")).isDisplayed())
        {
            System.out.println("Payment is successful");
            Reporter.log("Payment is successful");
        }
        else
        {
            Thread.sleep(2000);
            {
            if(Driver.driver.findElement(By.id("pay_decline")).isEnabled()) 
             {
                System.out.println("pay declined");
                action.moveToElement(By.id("pay_decline")).isEnabled()).click().perform();
                Reporter.log("PAYMENT DECLINED!!");
              }

         }  
    catch(ExceptionInInitializerError ex)
    {
            System.out.println(ex);
    }
  }

I am getting an error saying:

Unable to locate element: {"method":"xpath","selector":"//div[@id='Tpay_succes']"}

I want the else block to be executed if the if block doesn't get executed. Any suggestions are welcome. Thanks.

6
  • 2
    What is your question? Commented Apr 10, 2019 at 20:35
  • code hard to read, considering format/indentation (eventually part of the problem, which we don't know). Example brackets same line after else and in front of if Commented Apr 10, 2019 at 20:37
  • @KorayTugay I edited it Commented Apr 10, 2019 at 20:40
  • "I want to the else block to be executed if the if block doesn't get executed. " That's exactly how if...else works. Commented Apr 10, 2019 at 20:50
  • 1
    still not correct, bracket at end of second if, and in general closing is not matching opening, neither is indentation Commented Apr 10, 2019 at 21:30

1 Answer 1

1
if(Driver.driver.findElement(By.xpath("//div[@id='Tpay_success']")).isDisplayed())

This doesn't work the way you think it does. findElement() will throw an error if the element is not present in the DOM of the currently loaded page. This means that isDisplayed() will not be called in such a case.

You can do what you want with a try...catch:

  try
    {
       Driver.driver.findElement(By.xpath("//div[@id='Tpay_success']"))
       System.out.println("Payment is successful");
       Reporter.log("Payment is successful");
    } catch(ExceptionInInitializerError ex) {
            Thread.sleep(2000);
            if(Driver.driver.findElement(By.id("pay_decline")).isEnabled()) {
                System.out.println("pay declined");
                action.moveToElement(By.id("pay_decline")).isEnabled()).click().perform();
                Reporter.log("PAYMENT DECLINED!!");
            }
    }

Note that you should learn about how to make the selenium driver wait for a specific element rather than using Thread.sleep().

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the response. Another issue that I'm facing is that, by the time it's reaching the catch block, its getting too late, meaning the submit button with the div id='Tpay_success', in the catch block, is getting lost, when it's reaching it. Is there a way of making it faster, I have removed the Thread.sleep() method.
is this kind of indentation and use of brackets really used with selenium? opening/closing sometimes on extra line, sometimes on same line; opening not matching closing neither indentation??? that's so confusing
The indentation doesn't matter that much in Selenium but since I'm using with Java, that's the reason the brackets are needed.
nor does it matter in Java, but it is essential for easy reading/understanding (almost any programing language) - and I doubt that in Selenium you dont need to match opening and closing bracket; e.g. opening on end of if, closing on same indent level as catch (which by itself is not being closed)
Yes Selenium doesn't need anything like that, and I have rectified.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.