atkHOBO
atkHOBO

Reputation: 83

How to click an image with Selenium with only an SRC

<td colspan="2" align="center">
        <img src="web/L001/images/IMAGENAME.jpg" width="213" height="46" border="0" onclick="javascript: MessageDisplay()" ></td>

This is the element I'm trying to click. My attempted code:

WebElement temp = driver.findElement(By.xpath("web/L001/images/Phishing_12.jpg"));
temp.click();

I even tried with the full address, but any ides would be appreciated.

I use this to log on to various websites but this particular one throws up a web-page before that I have to click on that element before I can continue. -Thx

Upvotes: 8

Views: 81042

Answers (2)

JacekM
JacekM

Reputation: 4099

Generally CSS selectors are favored over xpaths. That's why I would recommend:

WebElement temp = driver.findElement(By.cssSelector("img[src='web/L001/images/IMAGENAME.jpg']"));

Upvotes: 8

Amey
Amey

Reputation: 8548

This xpath should find it

WebElement temp = driver.findElement(By.xpath("//img[@src='web/L001/images/IMAGENAME.jpg']"));

or use contains like so

WebElement temp = driver.findElement(By.xpath("//img[contains(@src,'web/L001/images/IMAGENAME.jpg')]"));

But i think the problem would be is that you are not waiting for the element.

Upvotes: 16

Related Questions