0

我有一个块:

@Block(@FindBy(xpath = "//tr[contains(@class,'bg-success')]"))
public class ShareContentRowBlock extends HtmlElement {

   @FindBy(xpath = "//h3[@class='fileName']/span/a")
   private TextBlock contentNameText;

   public String getContentName() {
       return contentNameText.getText();
   }

   .... // some other elements and methods
}

我描述了一个页面:

public class DocumentLibraryPage extends SitePage {

    private List<ShareContentRowBlock> shareContentRowBlocks;

    .....

    public ShareContentRowBlock getShareContentRowBlock(String name){
        for (ShareContentRowBlock conentRowBlock: shareContentRowBlocks){
            if(name.equals(conentRowBlock.getContentName())){
                return conentRowBlock;
            }
        }
        return null;
    }
}

当我尝试获取元素时,它返回的不是我想要查看的元素。

我有带有元素树的html:

   html
       h3.fileName
         span 
             a
       h3.fileName
         span 
             a
       table.bg-success
         h3.fileName
             span 
                 a

我想<a>在表格中获取元素,但它返回所有 3 个<a>元素。当我尝试调试时,它确实找到了所有<a>忽略父块 xpath 的元素。

它有什么问题?我是否需要更改选择器,或以其他方式描述块?

4

1 回答 1

1

以“ // ”开头的 xpath 定位器表示绝对块位置。为了进行相对搜索,您应该以“ . ”开头:

@Block(@FindBy(xpath = "//tr[contains(@class,'bg-success')]"))
public class ShareContentRowBlock extends HtmlElement {

   @FindBy(xpath = ".//h3[@class='fileName']/span/a")
   private TextBlock contentNameText;

   public String getContentName() {
       return contentNameText.getText();
   }

   .... // some other elements and methods
}
于 2014-10-16T12:52:06.147 回答