I've been using @FindBy
for a while now, and I love the fact that the element doesn't get located until its necessary (not on instantiation).
However, the webpage may have anywhere from 2-10 of a certain element, and the id's on the elements are numbered (so the first element has an id of "element1" and so forth)
I would like to write a function where I can pass in an integer, and it will return a WebElement with the appropriate ID, AND is lazily instantiated. That means having a function like the following won't work:
public WebElement getElement(int numOnPage){
return driver.findElement(By.id("element"+numOnPage));
}
Because the instant I call that function the WebElement gets located. (The reason why it can't be instantiated is because I have a function that waits until it the element exists by calling isDisplayed() over and over on it, catching NoSuchElementException
s).
I also realize that I could create a List<WebElement>
that selects via CSS every element whose ID starts with "element" but I have had other cases where I've wanted to return a dynamically generated element, and had to use a workaround there as well.
Thanks!