简体   繁体   English

对在Page Factory Model + Selenium Java中创建的Web元素进行通用化xpath

[英]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.

相关问题 网页对象模型和网页工厂(如果是网页元素列表) - Page Object Model and Page Factory in case of list of web elements 如何使用JAVA在Selenium WebDriver中通过XPath定位Web元素? - How to locate web elements by XPath in Selenium WebDriver using JAVA? Selenium Grid与页面对象模型页面工厂 - Selenium Grid With Page Object Model Page Factory 使用 Page Factory Design 和 Page Object Model 进行测试,使用 Selenium 和 ZD5238788375ZA21833187880E1EA218 打开浏览器的两个实例 - Tests using Page Factory Design and Page Object Model opens two instances of the browser using Selenium and Java xPath适用于最后一页,但不适用于第一页-Selenium Java - xPath is working for the last page but not the first - Selenium Java 硒中的页面对象模型和页面工厂有什么区别? - What is the differnce between page object model and page factory in selenium? Java,用于查找Web元素的相对xpath - Java, relative xpath to find web elements 在Java页面对象模型中支持两个版本的网页的工厂设计模式 - Factory design pattern for supporting two version of web pages in java page object model 如何在.xpath中找到Selenium Web Driver中的元素 - How to find elements in Selenium Web Driver by.xpath Selenium Webdriver:使用相对于其他元素的路径进行页面工厂初始化? - Selenium Webdriver: Page factory initialization using paths relative to other elements?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM