2014-10-16 59 views
0

搜索时,只有一个块我有一个块:HTML元素发现3个元素,而不是使用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 
} 

我描述了一个页面:

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。

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

回答

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 
}