Search code examples
javaseleniumselenium-webdriverwebautomation

how to find an image tag using selenium webdriver in java and how to check if it has been properly displayed?


I need to check a webpage for all the images and get their attributes like src and width etc using selenium webdriver. Can someone please help me with it ?


Solution

  • I will give you few ideas:

    1st I would start with findElements method which returns all elements:

     List<WebElement> allImages = driver.findElements(By.ByTagName("img"));
     List<String> widthofImage = new ArrayList<String>;
     List<String> heighthOfImage = new ArrayList<String>;
    

    Then you will have to iteratwe through the list

     for (WebElement imageFromList: allImages){
       widthOfImage.add(imageFromList.getAttribute("width"));
       heighthOfImage.add(imageFromList.getAttribute("height"));
       //.. etc
    
     }
    

    And then you have these values stored :) You can then iterate throuh them using same way as noted above...

    NOTE I am writing this from top of the head. So please double check the code

    BTW the driver variable is assumed healthy and living instance of WebDriver