Selenium: How do I get the src of an image?

前端 未结 9 1017
抹茶落季
抹茶落季 2020-12-17 09:32

I\'m using Selenium (within PHPUnit) to test my web application. I would like to test whether a certain image which is present on the page really exists. More precisely, whe

9条回答
  •  无人及你
    2020-12-17 10:13

    Well, in chrome one can write code in java to get the list of image src

    import java.util.List;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    
    public class imageUrl {
        public static void main(String[] args) {
    
                    //Create Driver-object for chrome browser
                    System.setProperty("webdriver.chrome.driver","//Users//admin//Desktop//chromedriver");
                    WebDriver driver = new ChromeDriver();
    
                    //get the page
                    driver.get("https://jet.com/");
    
                    // this will find the elements by tag name. Don't forget to write "WebElement" after List.
                    //or else it will give an error on the for loop
    
                    Listlinks=driver.findElements(By.tagName("img"));
    
                    // this will display list of all images exist on page
                    for(WebElement ele:links){
                        System.out.println(ele.getAttribute("src"));
                    }
        }
    
    }
    

提交回复
热议问题