Reputation: 378
I have dynamically generated locators depends on which element is gonna be chosen from a list.
For instance, this is my generated id:
Function:TableName:0:submenuAction
0 is the counter, meaning first element is chosen from the list. All strings before and after the counter won't change, so start with, contains xpath method does not work.
Can I use some wildcard like
Function:TableName:{X}:submenuAction
{x} is integer.
I can store the chosen number of element, but how can I use this variable inside the pagefactory element? driver.findelement can fail with staleElementException,thats why I wanna use PageFactory.
Upvotes: 0
Views: 1463
Reputation: 256
Whatever you're passing in would probably have to be a constant (e.g. final static
in Java) to be used in a PageFactory
annotation.
private final static int ITEM_INDEX = 2;
...
@FindBy(xpath="Function:TableName:"+ ITEM_INDEX + ":submenuAction")
private WebElement targetListItem;
In addition to some of the stale checking strategies mentioned in the comment, you may also want to try binding all of those dynamically-identified elements to a collection (e.g. List<WebElement>
), then indexing in:
@FindBy(xpath="...")
private List<WebElement> allListEntries;
...
private WebElement getTargetListEntry(int index) {
return allListEntries.get(index);
}
Upvotes: 3