使用XPath进行搜索时,HtmlElements可以找到3个元素,而不是块中的一个元素 [英] HtmlElements finds 3 elements instead of only one in block when searching with 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;
}
}
当我尝试获取元素时,它返回的不完全是我想要看到的元素.
When I try to get element, it returns not exactly element that I want to see.
我有带有元素树的html:
I have html with elements tree:
html
h3.fileName
span
a
h3.fileName
span
a
table.bg-success
h3.fileName
span
a
我想在表中获取元素<a>
,但是它返回所有3个<a>
元素.
当我尝试调试时,它实际上会忽略父块xpath来查找所有<a>
元素.
I want to get element <a>
inside table, but it returns all 3 <a>
elements.
When i try to debug it really finds all <a>
elements with ignoring parent block xpath.
这有什么问题?我需要更改选择器,还是以其他方式描述块?
What wrong with it? Am I need to change selectors, or describe block in other way?
推荐答案
以"//"开头的xpath定位器表示绝对块位置.为了进行相对搜索,您应该以"."开头:
Starting xpath locator with "//" means absolute block position. In order to make relative search you should start it with ".":
@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
}
这篇关于使用XPath进行搜索时,HtmlElements可以找到3个元素,而不是块中的一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!