首页 > 解决方案 > selenium 找不到 Angularjs 元素 - Java

问题描述

我需要从页面中的特定元素中获取文本,这些元素只有唯一的标签“ng-bind="$Gadget.customerSegmentation",并且元素位置可能会根据场景进行更改

我尝试在下面查找元素,但有时它有时不起作用。

@FindBy(xpath = "//div[@ng-bind='$Gadget.customerSegmentation']")
WebElement Customer;

<div class="form_group inline_block" style="width:30%;"> 
  <div class="form_label ng-binding" style="width:30%;">Cust Segmentation:</div> 
   <div class="form_control ng-binding" title="Low" ng-bind="$Gadget.customerSegmentation" style="width:65%;float:right;">Low</div>  
                </div>

即使位置改变了也能找到元素。

标签: javaangularjsseleniumxpathcss-selectors

解决方案


要识别元素,您可以使用以下任一Locator Strategies

  • 选择器

    @FindBy(css  = "div.form_control.ng-binding[title='Low'][ng-bind$='customerSegmentation']")
    WebElement Customer;
    
  • 路径

    @FindBy(xpath = "//div[@class='form_control ng-binding' and @title='Low'][contains(@ng-bind, 'customerSegmentation')]")
    WebElement Customer;
    

推荐阅读