Rupali
Rupali

Reputation: 13

How to click on an image in selenium webdriver

I've been trying to click on an image on web page. Xpath for this image is:

//*[@id='gridview-1018']/table/tbody/tr[3]/td[7]/div/a/img

HTML code is:

<td class=" x-grid-cell x-grid-cell-gridcolumn-1016 ">`<div class="x-grid-cell-inner " style="text-align: center; ;">`<a href="http://demo.webshopondemand.com/Shop/AbzorbDevelopment/Store/" target="_blank">`<img src="/admin/templates/images/house.png" style="background-color: transparent;"/>`

Tried all the below methods here, but got the same error msg "Unable to locate the element:

  1. driver.findElement(By.xpath(".//*[@id='gridview-1018']/table/tbody/tr[3]/td[7]/div/a/img")).click();

  2. WebElement temp = driver.findElement(By.xpath("//img[contains(@src,'/admin/templates/images/house.png')]")); temp.click();

  3. WebDriverWait wait = new WebDriverWait(driver, 60); wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("x-grid-cell-inner.a.img"))); driver.findElement(By.cssSelector("x-grid-cell-`inner.a.img")).click()

4.driver.findElement(By.cssSelector`("a[href='AbzorbDevelopment']")).click();

Thanks for the helpenter image description here

Upvotes: 1

Views: 25071

Answers (2)

Rajnish Kumar
Rajnish Kumar

Reputation: 2938

Hi please use Actions class with below syntax

Actions act = new Actions(driver);
act.moveToElement(xpath).click().build().perform();

Upvotes: 0

Priya P
Priya P

Reputation: 123

Do as what raj said i.e. use Actions class

Actions act = Actions(driver); act.moveToElement(xpath).click().build().perform();

But instead of such absolute xpath, can you try for CSS as given below:

div.x-grid-cell-inner>a[href='http://demo.webshopondemand.com/Shop/AbzorbDevelopment/Store/']>img[src='/admin/templates/images/house.png']

your final implementation will be like:

WebElement shop = driver.findElement((By.css("div.x-grid-cell-inner>a[href='http://demo.webshopondemand.com/Shop/AbzorbDevelopment/Store/']>img[src='/admin/templates/images/house.png']"))); 

Actions actions = new Actions(driver); actions.moveToElement(shop).click().build().perform();

Please note: above css path is created based on source code you had given in your question.

Upvotes: 0

Related Questions