我怎样才能将src作为属性

时间:2015-02-16 00:34:47

标签: selenium selenium-webdriver

我需要getAttribute(" src")图片。当我运行我的脚本时,它会显示" null"。行动吧,行动起作用了!

我的剧本:

WebElement image = driver.findElement(By.id("creativeLink"));
verifyDisplay(image.getAttribute("src"), By.id("creativeLink"));
action.moveToElement(image).perform();

页面HTML:

<div id="creativeContent">
<table>
<tbody><tr>
<td>

<a id="creativeLink" href="http://www.website.com" target="_new"> <img href="http://www.website.com" rel="faceboxMO" alt="" src="https://console.data.com/images/login/logo.png" height="50" border="0" width="50"></a>
</td>
<td valign="top"><div class="hint">(Note: Mouse over creative to see in correct dimensions)</div></td>
</tr>
<tr>
<td>
</td></tr></tbody></table>
</div>

1 个答案:

答案 0 :(得分:1)

看起来你在那里使用了错误的选择器。由于您使用的是某些自定义功能,因此我无法对其进行测试。但是,我提出了类似跟随的东西,看起来很有效。正确的选择器应如下所示:

// The selector finds the img element
By by = By.cssSelector("#creativeLink>img");

//Getting attribute on on img tag
String src = driver.findElement(by).getAttribute("src");

System.out.println(src);

打印

https://console.data.com/images/login/logo.png

可能会做这样的事情,它会起作用:

WebElement image = driver.findElement(By.cssSelector("#creativeLink>img"));
verifyDisplay(image.getAttribute("src"), By.cssSelector("#creativeLink>img"));
action.moveToElement(image).perform();