如何在selenium webdriver中单击图像

enter image description here 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代码是 -

<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;"/>`

尝试了以下所有方法,但得到了相同的错误信息“无法找到元素: -

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

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

  • WebDriverWait wait = new WebDriverWait (driver,60); wait.until( ExpectedConditions.visibilityOfElementLocatedBy.cssSelector ("x-grid-cell-inner.a.img"))); driver.findElementBy.cssSelector ("x-grid-cell-`inner.a.img")) . click()

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

谢谢您的帮助!

回答(2)

3 years ago

嗨,请使用以下语法的Actions类

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

3 years ago

像raj所说的那样使用Actions类

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

但是,如果没有这样的绝对xpath,你可以尝试使用CSS,如下所示:

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

你的最终实现将是:

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.moveToElement(商店) . 单击() Build ()执行();

请注意:上面的css路径是根据您在问题中提供的源代码创建的 .