例如,
WebElement parentEle = driver.findElement(By.id("xyz"));
WebElement childEle = parentEle.findElement(By.id("abc"));
childEle.click();
在上面的例子中,我们在parentEle中找到了childEle。 我们如何使用@FindBy注释(与PageFactory结合)实现这一目标
答案 0 :(得分:1)
首先,如果孩子有一个独特的ID,为什么你需要从父母那里找到孩子? id的主要目的是提供使用唯一选择器查找元素的灵活性。
如果子元素确实与其他元素嵌套,请使用xpath查找。我经常使用它。
@FindBy(how = How.XPATH, using = "//something/something")
private WebElement TestElement;
或ID
@FindBy(how = How.ID, using = "abc")
private WebElement TestElement;
答案 1 :(得分:0)
@FindBy (id = "abc")
private WebElement TestElement;
在您的页面工厂中尝试此操作并从测试方法中调用该方法。
public WebElement Child_Of_ParentElement(WebElement TestElement, String Child_Id)
{
return TestElement.findElement(By.id(Child Id));
}
答案 2 :(得分:0)
这可以通过几个步骤来实现:
//identify parent element with @FindBy annotation
1) @FindBy(className="locator")
List<WebElement> parent;
//loop through each parent to get child(<li> in your case) of each parent
2) for(WebElement list_items:Parent){
list_items.findElement(By.xpath("child locator"));
我使用过这种方法,并且能够达到预期的效果。希望这也能解释您的疑问。
答案 3 :(得分:0)
正如其他答案所述,我会使用xpath,尽管你必须重写每个子元素的根路径,除非你使一个字符串保持不变或者其他什么,但这可能会开始变得混乱。
// Child 1
@FindBy(xPath = "//[@id='xyz']/[@id='abc']";
private WebElement childAbcOfParentXyz;
// Child 2
@FindBy(xPath = "//[@id='xyz']/[@id='def']";
private WebElement childDefOfParentXyz;