Webdriver/Pageobject/Findby: How to Specify Xpath With Dynamic Value

WebDriver/PageObject/FindBy: how to specify xpath with dynamic value?

Thanks to Ardesco and Robbie, I came up with the following solution:

private String RequiredSystemNameXpath = "//td[contains(text(),'xxxxx')]";

private WebElement prepareWebElementWithDynamicXpath (String xpathValue, String substitutionValue ) {

return driver.findElement(By.xpath(xpathValue.replace("xxxxx", substitutionValue)));
}

public void deleteSystem (String systemName) {


WebElement RequiredSystemName = prepareWebElementWithDynamicXpath(RequiredSystemNameXpath, systemName);

RequiredSystemName.click();

}

Pagefactory dynamic webelement with wildcard

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);
}


Related Topics



Leave a reply



Submit