Reputation: 525
I am working on selenium Page Factory model.I need to generalize the xpath in such a way it should be matching according to user requirement.
Let's say i have following xpath
@FindBy(xpath = "(//div[@class='del-hours' and contains(.,'Store #50')]//..//..//..//..//i)[2]")
WebElement selectStore;
I need this xpath should be used for Store#44 or Store #100 or anyother according to user input.
With regular old way of findElementBy i can able to achieve this. But i would like to know with page factory concept can we achieve this ?
Thanks
Upvotes: 2
Views: 575
Reputation: 1270
I had the similar use case for one of my project. Here's what, I have used:
public RadioElement getTemplates(String id) {
return createBaseElement(By.id("templateId")).createRadioElement(By.xpath("//input[contains(@value,'1')]"));
}
In the above example : 1. ElementFactory.createBaseElement(By.id("lia-templateId")) // this is the parent class which helps in locating it's child element and is one of the best case. Use can use the similar for your use case as well, pass the locator for the parent having either div/id/classname .
2. createRadioElement(By.xpath("//input[contains(@value,'1')]")); // pass the sub class xpath details here I am passing the value as dynamic from the function based on user input call.
For your case, you have to do somewhat similar changes :
public RadioElement getTemplates(String id) {
@FindBy(xpath = "(//div[@class='del-hours' and contains(.,'Store #id')]//..//..//..//..//i)[2]")
WebElement selectStore;
}
Try modify the path, as path does not seems to proper you can use
(By.xpath("//div[contains(@class,'del-hours')]"))
Upvotes: 2