当元素是同一元素时,可能导致元素无法单击的是什么



img WebElement单击很少失败,即使单击的元素是相同的元素,也不可单击。

我有一个包含一些按钮的对话框(其中包含IMG X的关闭按钮(。

当我尝试通过单击IMG的WebElement单击"对话框"时,很少会得到WebDriverException,如下所示:

由:org.openqa.selenium.webdriverexception:未知错误:元素

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QAAKqNIzIAAAAJcEhZcwAACxIAAAsSAdLdfvwAAAAHdElNRQfhCB8QKRBsEUJwAAAAn0lEQVQoz72RMQ7CMAwADwbgCSyhqmDpFj6AWqSOfIoH8BkGNsLGhiwG6NKJDvwBBoaExPCAeontu0SWA32F1HKTRawyucjK58PQ21LgvCIZjiU7DwbhzpQjBR0lbxw5LaXtlKCUl8ZKiAoapxkARkzCOU7NKMiMEzktdwxO5n9CxBVrGq18Xzh4bB/2SUWDYf+7qI1cxaRx5Sx1b7/0AYDRNbDgNXvDAAAAAElFTkSuQmCC" class="gwt-Image" style="visibility: visible;">在点(834,307(处不可单击。其他元素会单击:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QAAKqNIzIAAAAJcEhZcwAACxIAAAsSAdLdfvwAAAAHdElNRQfhCB8QKRBsEUJwAAAAn0lEQVQoz72RMQ7CMAwADwbgCSyhqmDpFj6AWqSOfIoH8BkGNsLGhiwG6NKJDvwBBoaExPCAeontu0SWA32F1HKTRawyucjK58PQ21LgvCIZjiU7DwbhzpQjBR0lbxw5LaXtlKCUl8ZKiAoapxkARkzCOU7NKMiMEzktdwxO5n9CxBVrGq18Xzh4bB/2SUWDYf+7qI1cxaRx5Sx1b7/0AYDRNbDgNXvDAAAAAElFTkSuQmCC" class="gwt-Image" style="visibility: visible;">

您可以看到元素是相同的。我开始怀疑该元素的陈旧性,其支持,甚至元素移动的可能性。

下面是一个代码段,包括我要解释时调试消息。

private static final By X_BUTTON__SELECTOR
    = WlSeleniumUtils.selectTagWithClass("img", "gwt-Image");
public void clickAndReturnIfAny() {
   WebElement closeImage = manageWorkspaceDialogWebElem
     .findElement(X_BUTTON__SELECTOR);
   // print the elements location before clicking
   System.out.println(String.format
     ("Position: %s, Dimensions: %s", 
      closeImage.getLocation() + "", 
      closeImage.getSize() + ""));
   try {
     // print check for staleness and enablement.
     System.err.println
         ("close button: stale? "
          + driverHelper.isStale(closeImage)
          + " displayed? " + closeImage.isDisplayed()
          + " enabled? " + closeImage.isEnabled()
          + " clickable? " + (ExpectedConditions
                              .elementToBeClickable
                              (closeImage)
                              .apply(driverHelper.getDriver())
                              != null));
     closeImage.click();
   }
   catch (WebDriverException wde) {
     System.err.println
         ("Faied to click manage-workspaces dialog's "
          + "close button: stale? "
          + driverHelper.isStale(closeImage)
          + " displayed? " + closeImage.isDisplayed()
          + " enabled? " + closeImage.isEnabled()
          + " clickable? " + (ExpectedConditions
                              .elementToBeClickable
                              (closeImage)
                              .apply(driverHelper.getDriver())
                              != null));
     // print the entire HTML content to check for multiple img tags.
     System.err.println
         (manageWorkspaceDialogWebElem.getAttribute("innerHTML"));
     // fetch the element again and check its location
     closeImage = manageWorkspaceDialogWebElem
         .findElement(X_BUTTON__SELECTOR);
     System.out.println(String.format
             ("Position: %s, Dimensions: %s",
              closeImage.getLocation() + "",
              closeImage.getSize() + ""));
     throw new RuntimeException
         ("Failed to close mange-workspaces dialog.", wde);
   }
}

执行点击之前:

位置:(826,299(,尺寸:(16,16(

关闭按钮:陈旧?虚假显示?启用了吗?真正可单击?true

执行单击并捕获例外之后:

位置:(826,299(,尺寸:(16,16(

因此该元素尚未移动。

您可以看到内部HTML只是一个IMG:

<div class="popupContent">
   <table cellspacing="0" cellpadding="0" style="background-color: rgb(255, 255, 255); width: 100%; height: 100%;">
      <tbody>
         <tr>
            <td align="right" style="vertical-align: top;"><button type="button" class="wl-popup-close-button" style="border-style: none; outline-style: none; background-color: rgb(255, 255, 255); padding: 0px;"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QAAKqNIzIAAAAJcEhZcwAACxIAAAsSAdLdfvwAAAAHdElNRQfhCB8QKRBsEUJwAAAAn0lEQVQoz72RMQ7CMAwADwbgCSyhqmDpFj6AWqSOfIoH8BkGNsLGhiwG6NKJDvwBBoaExPCAeontu0SWA32F1HKTRawyucjK58PQ21LgvCIZjiU7DwbhzpQjBR0lbxw5LaXtlKCUl8ZKiAoapxkARkzCOU7NKMiMEzktdwxO5n9CxBVrGq18Xzh4bB/2SUWDYf+7qI1cxaRx5Sx1b7/0AYDRNbDgNXvDAAAAAElFTkSuQmCC" class="gwt-Image" style="visibility: visible;"></button></td>
         </tr>
      </tbody>
   </table>
</div>

因此,关于为什么发生这种情况的任何建议将不胜感激。

尝试诱导java脚本执行程序单击以下xpath。

WebElement element=driver.findElement(By.xpath("//button[@class='wl-popup-close-button']/img[@class='gwt-Image']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);

可能是<img>本身不可单击的情况,您需要单击父 <button>

在XPath中,您可以访问DOM及其父母,孩子,兄弟姐妹等中的任何元素。

因此,我建议您修改您的选择器指向父按钮:

//img[@class='gwt-Image']/parent::button

我的期望是点击应该成功。

参考:

  • xpath教程
  • XPath轴
  • XPath操作员&amp;功能

相关内容

  • 没有找到相关文章

最新更新