[英]Generalize xpath for Web elements created in Page factory Model + selenium Java
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. 我正在硒页面工厂模型上工作。我需要对xpath进行概括,使其可以根据用户要求进行匹配。
Let's say i have following xpath 假设我有以下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. 我需要根据用户输入,此xpath应该用于Store#44或Store#100或其他。
With regular old way of findElementBy i can able to achieve this. 使用常规的findElementBy旧方法,我可以实现这一目标。 But i would like to know with page factory concept can we achieve this ?
但是我想知道页面工厂的概念可以实现吗?
Thanks 谢谢
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. 在上面的示例中:1. ElementFactory.createBaseElement(By.id(“ lia-templateId”)) // //这是父类,可帮助查找其子元素,并且是最好的情况之一。 Use can use the similar for your use case as well, pass the locator for the parent having either div/id/classname .
Use也可以在您的用例中使用类似的方法,为具有div / id / classname的父级传递定位器。
2. createRadioElement(By.xpath("//input[contains(@value,'1')]")); 2. createRadioElement(By.xpath(“ // input [包含(@ value,'1')]”)))); // pass the sub class xpath details here I am passing the value as dynamic from the function based on user input call.
//在此处传递子类xpath详细信息我正在基于用户输入调用从函数传递值作为动态值。
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')]"))
尝试修改路径,因为您似乎不能使用该路径
(By.xpath("//div[contains(@class,'del-hours')]"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.