1

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

2 Answers 2

0

Hi please use Actions class with below syntax

Actions act = new Actions(driver);
act.moveToElement(xpath).click().build().perform();
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks Raj for your reply. I tried following code: WebElement shop = driver.findElement((By.xpath("//*[@id='gridview-1018']/table/tbody/tr[3]/td[7]/div/a/img"))); Actions actions = new Actions(driver); actions.moveToElement(shop).click().perform(); Received error msg: Unable to locate element: {"method":"xpath","selector":"//*[@id='gridview-1018']/table/tbody/tr[3]/td[7]/div/a/img"}
Received error msg: Unable to locate element: {"method":"xpath","selector":"//*[@id='gridview-1018']/table/tbody/tr[3]/td[7]/d‌​iv/a/img"}
means your xpath is invalid plz try to give valid xpath it will surely work
xpath is .//*[@id='gridview-1018']/table/tbody/tr[3]/td[7]/div/a/img
form here i cannot validate your xpath as i donot have source code of your page if possible provide me the link
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.