Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If else condition else is not working. Java Selenium

I used this condition to get image and image text. But else statement is not working.

if(mercuryImage.isDisplayed());

        {
            System.out.println("Imageis displayed");
            //Get image text
            System.out.println("The text of image "+ mercuryImage.getAttribute("alt"));
        }
        else
        {
            System.out.println("Image is not displayed");
        }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Else statements execute when the condition inside the if statement returns or equals to false.

I didn’t see any errors in your code except for that it’s a bit unreadable for some people.

(And that dangling ; by your if statement, don’t know who taught you to do that.)

Here’s a much more readable take:

if (mercuryImage.isDisplayed()) {
  System.out.println("Image is displayed.");
  //Get image text
  System.out.println("The alt text of the image "+ mercuryImage.getAttribute("alt"));
} else {
  System.out.println("Image is not displayed");
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading