如果不编写自定义内容,您想要做的事情是不容易实现的ElementLocatorFactory
.
首先我真的推荐使用XPath。
这将使您很容易获得:
3rd <table>
像这样:@FindBy(xpath = "\\table[3]")
and...
2nd <li>
在第三张表中就像这样:@FindBy(xpath = "\\table[3]\li[2]")
.
但如果你真的想用更短的时间来做@FindBy
注释,你可以去ElementLocatorFactory
.
public class FindByContextModifier implements ElementLocatorFactory {
private final SearchContext context;
public FindByContextModifier(final SearchContext context) {
this.context = context;
}
public ElementLocator createLocator(final Field field) {
return new DefaultElementLocator(context, field);
}
}
带有将为您提供上下文的元素的类:
public class Parent {
@FindBy(name = "myTable")
WebElement table;
public WebElement getTable() {
return this.table;
}
}
它的孩子:
public class Child {
@FindBy(name = "particular")
WebElement specialTableListElement;
}
Usage:
Parent parent = PageFactory.initElements(driver, Parent.class);
FindByContextModifier parentContext = new FindByContextModifier(parent.getTable());
Child child = new Child();
// This will look for the name "particular" inside the element with "myTable" name
PageFactory.initElements(parentContext, child);