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
Sharing a Variable Between Multiple Different Threads
How to Center Crop a Background Image of Linear Layout
Java.Sql.Sqlexception: Field Doesn't Have a Default Value
Simplifying the If-Else Condition Using Java Functional Style Programming
Keytool Error Bash: Keytool: Command Not Found
Scheduling a Job With Spring Programmatically (With Fixedrate Set Dynamically)
Typecasting an Object from Parent Class to Child
Spring Boot Application Shutdown Immediate After Starting
How to Change Cookie Processor to Legacycookieprocessor in Tomcat 8
Spring Webclient: How to Stream Large Byte[] to File
Deserialization With @Jsonsubtypes for No Value - Missing Property Error
How to Do Name Validation Allowing Alphabet, Spaces and Dot in Android
Automatically Convert Style Sheets to Inline Style
Splitting a String Containing Special Characters in to Individual Characters
How to Access Variable Outside a Try Catch Block
Spring Data JPA Problem With Updating Multiple Entities
Java.Time.Format.Datetimeparseexception: Text Could Not Be Parsed At Index 3