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

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

    If You are you using any PHP webdriver like facebook/phpwebdiver, then it will solve your problem very easily.

    Example :

    1) Create a wedriver object:

    public function setUp()
    {
    parent::setUp();
    $web_driver = new WebDriver();
    $element = $web_driver->session();
    }
    

    With this $element variable you can easily access any HTML attribute with the following line:

    $element->element('xpath', '//*[@id="logo"]')->attribute('src');
    

    This will return you the value of src attibute. You can use this line to get any attribute like class, id, title, alt, etc...

提交回复
热议问题