在角度站点中使用xpath时找不到元素

时间:2018-06-07 15:56:13

标签: java selenium xpath

我有一个有角度的网站,我正在尝试使用Selenium / Java自动化它。我知道量角器对角度位置更容易,但我希望使用硒。

我一直在使用"包含" xpath中的关键字用于查找元素,因为没有唯一的ID可用。

我遇到问题的元素是附在红色圈中的图像。当我使用xpath搜索控制台时,如图所示,元素将突出显示。但是当我在代码中使用它时,我发现元素未找到错误。

有没有更好的方法来处理这个以及为什么我收到错误。我已经有了等待条件。

enter image description here

HTML代码:

<div class="status-selector ng-isolate-scope" fm-select="" 
     fm-select-options="::IssueDetailsCtrl.issue.allowedIssueStatuses" 
     fm-disabled="!IssueDetailsCtrl.issue.permissions.editStatus" 
     fm-model="IssueDetailsCtrl.issue.status" 
     fm-change="IssueDetailsCtrl.updateStatus()">
  <div class="fm-select undefined selected" ng-class="getStyle()" tabindex="" 
       ng-keyup="handleKeys($event)" ng-keydown="handleKeyDown($event)" style="">
    <div class="fm-select-title" ng-click="toggleVisibility()" 
         ng-class="{&quot;fm-select-title-highlighted&quot;: 
                   isOpen, &quot;fm-select-disabled&quot;: fmDisabled }">
      <div class="selected-item-icon" ng-class="selectedOption.imageClass"></div>
      <span class="ng-binding">Open</span>
    </div>
    <!-- ngIf: isOpen -->
  </div>
</div>
<div class="fm-select undefined selected" ng-class="getStyle()" tabindex="" 
     ng-keyup="handleKeys($event)" ng-keydown="handleKeyDown($event)" style="">
  <div class="fm-select-title" ng-click="toggleVisibility()" 
       ng-class="{&quot;fm-select-title-highlighted&quot;: 
                 isOpen, &quot;fm-select-disabled&quot;: fmDisabled }">
    <div class="selected-item-icon" ng-class="selectedOption.imageClass"></div>
    <span class="ng-binding">Open</span>
  </div>
  <!-- ngIf: isOpen -->
</div>
<div class="fm-select-title" ng-click="toggleVisibility()" 
     ng-class="{&quot;fm-select-title-highlighted&quot;: 
               isOpen, &quot;fm-select-disabled&quot;: fmDisabled }">
  <div class="selected-item-icon" ng-class="selectedOption.imageClass"></div>
  <span class="ng-binding">Open</span>
</div>
<div class="selected-item-icon" ng-class="selectedOption.imageClass"></div>
<span class="ng-binding">Open</span>

等待条件:

 public void waitAndClickElement(WebElement element) throws InterruptedException {
        boolean clicked = false;
        int attempts = 0;
        while (!clicked && attempts < 20) {
            try {
                this.wait.until(ExpectedConditions.elementToBeClickable(element)).click();
                System.out.println("Successfully clicked on the WebElement: " + "<" + element.toString() + ">");
                clicked = true;
            } catch (Exception e) {
                System.out.println("Unable to wait and click on WebElement, Exception: " + e.getMessage());
                Assert.fail("Unable to wait and click on the WebElement, using locator: " + "<" + element.toString() + ">");
            }
            attempts++;
        }
    }

2 个答案:

答案 0 :(得分:1)

您的应用程序是Angular App,只有当Angular ng-if满足某些条件时,该按钮才动态附加到DOM树,因此需要等待Angular在附加到DOM树之前编译按钮的HTML。

WebDriverWait wait = new WebDriverWait(driver, 20);

WebElement ele = wait.until(ExpectedConditions.presenceOfElementLocated(
    By.xpath("//div/span[text()='Open']"))
);

ele.click();

答案 1 :(得分:0)

我建议你在这种情况下使用显式等待:

new WebDriver(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='ng-binding' and text()='Open']"))).click();  

您也可以使用此Xpath:

//div[@class='selected-item-icon']/following-sibling::span[text()='Open' and @class='ng-binding']