【问题标题】:How to click on an image in selenium webdriver如何在 selenium webdriver 中单击图像
【发布时间】:2016-08-07 03:38:37
【问题描述】:

我一直在尝试点击网页上的图片。 此图像的 Xpath 是:

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

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

  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 等待 = 新 WebDriverWait(驱动程序, 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();

感谢您的帮助

【问题讨论】:

    标签: selenium-webdriver


    【解决方案1】:

    您好,请使用以下语法的 Actions 类

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

    【讨论】:

    • 感谢 Raj 的回复。我尝试了以下代码: WebElement shop = driver.findElement((By.xpath("//*[@id='gridview-1018']/table/tbody/tr[3]/td[7]/div/a/ img")));动作动作=新动作(驱动程序); actions.moveToElement(shop).click().perform();收到错误消息:无法定位元素:{"method":"xpath","selector":"//*[@id='gridview-1018']/table/tbody/tr[3]/td[7] /div/a/img"}
    • 收到错误消息:无法定位元素:{"method":"xpath","selector":"//*[@id='gridview-1018']/table/tbody/tr [3]/td[7]/d‌​iv/a/img"}
    • 表示您的 xpath 无效请尝试提供有效的 xpath 它肯定会工作
    • xpath 是 .//*[@id='gridview-1018']/table/tbody/tr[3]/td[7]/div/a/img
    • 此处的表单我无法验证您的 xpath,因为如果可能的话,我没有您页面的源代码,请提供链接
    【解决方案2】:

    按照 raj 所说的去做,即 使用 Actions 类

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

    但是,你可以试试下面给出的 CSS,而不是这样的绝对 xpath:

    div.x-grid-cell-inner&gt;a[href='http://demo.webshopondemand.com/Shop/AbzorbDevelopment/Store/']&gt;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 动作 = new Actions(driver); actions.moveToElement(shop).click().build().perform();

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

    【讨论】:

      猜你喜欢
      • 2018-09-22
      • 1970-01-01
      • 2017-12-26
      • 2020-10-04
      • 2020-12-22
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      • 2017-05-21
      相关资源
      最近更新 更多