For example, I have an element1 which I can find after I find element2, like this:

driver.findElement(By.xpath("//xpath2")).findElement(By.xpath("//xpath1")).click();

I want to use PageObjectModel and annotations @FindBy, like these:

@FindBy(xpath = "//xpath1")
private WebElement element1;

@FindBy(xpath = "//xpath2")
private WebElement element2;

The problem: I don't know how to rewrite the code, this doesn't work:

element2.element1.click();
有帮助吗?

解决方案

@FindBy(xpath = ".//xpath1")
private WebElement element1;

@FindBy(xpath = ".//xpath2//xpath1")
private WebElement element2;

Use

element2.click();

其他提示

Well, you have a couple of solutions.

The first, if you want to use 2 FindBys is:

@FindBys({@FindBy(xpath = "//xpath2"),
       @FindBy(xpath = "//xpath1")})

The second is to put the second xpath code in an explicit findElement function

element1.findElement(By.xpath("//xpath1")).click();

Finally, you can probably combine the first xpath and the second into a single xpath

@FindBy(xpath="//xpath2//xpath1")

However, I imagine that you probably are asking this because you have a single element that you like to find lots of other elements within. That, unfortunately, is not possible with strictly @FindBy.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top