【问题标题】:How to find xpath or to findelement of tag input and type='image' in selenium如何在 selenium 中查找 xpath 或查找标签输入和 type='image' 的元素
【发布时间】:2019-12-10 17:04:08
【问题描述】:

我正在尝试自动化我遇到问题的 Web 应用程序。问题是我想单击一个按钮,即导出按钮,它将要求导出为 pdf 或 Excel。在检查时我可以找到该元素,但在运行脚本时它没有单击按钮。该按钮具有标签input 并键入image

我尝试过使用不同的 xpath,如下所示,我也尝试过没有点击按钮的绝对 xpath。

driver.findElement(By.xpath("//input[@name='exportReport']")).click();

WebDriverWait wait = new WebDriverWait (driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@name='exportReport']")));
driver.findElement(By.xpath("//input[@name='exportReport']")).click();

<td width="15px">
<input type = "image" name="exportReport" src="birt/images/ExportReport.gif title="Export report" alt="Export report" class="birtviewer_clickable">
</td>

这个标签不是img标签,但类型是image

我明白了:

“元素点击被拦截”、“NosuchelementException”。

【问题讨论】:

  • 你的浏览器是什么版本?
  • 您收到的是Element click Intercepted 还是NosuchelementException
  • 我用的是Chrome,版本是75.0.3770.142
  • 现在我得到 NosuchelementException。

标签: java selenium xpath css-selectors webdriverwait


【解决方案1】:

看来你很接近了。当您尝试在元素上使用 click() 而不是 EC 作为 visibilityOfElementLocated() 时,您需要使用 elementToBeClickable() 并且可以使用以下任一 Locator Strategies

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.birtviewer_clickable[name='exportReport'][src^='birt/images/ExportReport'][alt='Export report']"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='birtviewer_clickable' and @name='exportReport'][starts-with(@src, 'birt/images/ExportReport') and @alt='Export report']"))).click();
    

【讨论】:

  • 嗨,当我尝试这个时,它显示“org.openqa.selenium.ElementClickInterceptedException:”
  • @sai 查看更新的答案并告诉我状态
【解决方案2】:

您可以尝试使用下面给出的代码 sn-p

WebDriverWait wait = new WebDriverWait (driver, 20);

wait.until(ExpectedConditions.visibilityOf( 
driver.findElement(By.xpath("//input[@src='birt/images/ExportReport.gif']")));

driver.findElement(By.xpath("//input[@src='birt/images/ExportReport.gif']")).click();

【讨论】:

    【解决方案3】:

    确保您的元素不在iframe 内,如果是,您必须先切换到iframe,然后执行操作。如果没有,请尝试以下代码。

    WebDriverWait wait = new WebDriverWait (driver, 20);
    wait.until(ExpectedConditions.elementToBeClickable(By.name("exportReport"))).click();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-05
      • 1970-01-01
      • 1970-01-01
      • 2016-03-12
      相关资源
      最近更新 更多