Java 使用XPath搜索时,HtmleElements在块中查找3个元素,而不是仅查找一个

Java 使用XPath搜索时,HtmleElements在块中查找3个元素,而不是仅查找一个,java,selenium,xpath,htmlelements,Java,Selenium,Xpath,Htmlelements,我有一个街区: @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 getContentNam

我有一个街区:

@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;
    }
}
我想在表中获取元素
,但它返回所有3个
元素。 当我尝试调试时,它确实会找到忽略父块xpath的所有
元素


怎么了?我是否需要更改选择器,或以其他方式描述块?

以“/启动xpath定位器”表示绝对块位置。要进行相对搜索,应以“”开头:

   html
       h3.fileName
         span 
             a
       h3.fileName
         span 
             a
       table.bg-success
         h3.fileName
             span 
                 a
@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
}