我有一个 block :
@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>
忽略父 block xpath 的元素。
这有什么问题吗?我是否需要更改选择器,或以其他方式描述 block ?
最佳答案
以“//”开头的xpath定位器表示绝对 block 位置。为了进行相对搜索,您应该以“.”开头:
@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
}
关于java - 使用 XPath 搜索时,HtmlElements 在 block 中找到 3 个元素,而不是仅 1 个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26404866/