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

前端 未结 9 1006
抹茶落季
抹茶落季 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:17

    Assuming that you have an image in a WebElement (lets say img), in Java world you can retrieve the link below

    Editing the answer to clarify. By Java world I mean Selenium 2.0 Java bindings. In Selenium 2.0 (of course if you are using webdriver) has a class called WebElement representing elements on the page. getAttribute is a selenium Method in Java binding.

    String url = "http://www.my.website.com";
    WebDriver driver = new FirefoxDriver();
    driver.get(url);
    WebElement img = driver.findElement(By.id("foo"));
    String src = img.getAttribute("src");
    

    Perhaps there is something similar in PHPUnit

提交回复
热议问题