Search code examples
seleniumpage-factory

Selenium Page Factory - finding element with nested selectors


I'm trying to use Page Factory and @FindBy to initialize my WebElements. However, I'm running into trouble when I have an element that cannot be found with just the ID.

The following selector is a little more complex to find, so I'm using Selenium's ByChained:

val endreBoligModal: MutableList<WebElement> = driver.findElements(ByChained(By.className("hb-card"), By.className("hb-card-header"), By.className("hb-card-tittel"), By.tagName("span")))

The reason is that, for some reason, finding the element with a unique ID doesn't work. Selenium just cannot find it.

So, with regards to the Page Factory and the @FindBy way of creating elements - how do I do it?

The way I've used it so far is like this:

@FindBy(id = "login")
private WebElemement login

Or

@FindBy(css = "[id=login]")

By for the chained selector element, I cannot figure out how to do it with Page Factory.

I think there's something called @FindBys (with an s at the end). At least, there seems to be. But for the life of it I cannot find ANY documentation about it on the net, so I don't even know if it's relevant.

All help is appreciated.


Solution

  • You can just look at the sources of org.openqa.selenium.support.FindBys. There is a short but informative description saying:

    Used to mark a field on a Page Object to indicate that lookup should use a series of @FindBy tags in a chain as described in ByChained It can be used on a types as well, but will not be processed by default.

    Eg: @FindBys({@FindBy(id = "foo"), @FindBy(className = "bar")})

    So the example seems pretty straightforward. Give it a try.