Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 397
atkHOBO Avatar asked Aug 23 '13 16:08

atkHOBO


People also ask

How do I click an image in Selenium?

We can click on an image with Selenium webdriver in Python using the method click. First of all, we have to identify the image with the help of any of the locators like id, class, name, css, xpath, and so on. An image in the html code is represented by the img tagname. Let us see the html code of an image element.

Where is IMG SRC in Selenium?

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

How do you inspect an image in Selenium?

We can check if an image is displayed on page with Selenium. To verify an image we shall take the help of Javascript Executor. We shall utilize executeScript method to execute the Javascript commands in Selenium. Then pass the command return arguments[0].

How do you click an object in Selenium?

How to perform a Right Click in Selenium? For automating the right-click operation, Selenium provides a dedicated method – contextClick(). This method accepts the target WebElement as the argument. In order to use this method, use the Actions class object.


2 Answers

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.

like image 180
Amey Avatar answered Sep 20 '22 13:09

Amey


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']"));
like image 34
JacekM Avatar answered Sep 19 '22 13:09

JacekM