简体   繁体   English

将xpath从String转换为WebElement

[英]Converting xpath from String to WebElement

Right now I use the below method in my BasePage and I wish to call this method to my other pages. 现在,我在BasePage中使用以下方法,并且希望将此方法调用到其他页面。

So in the below method, the parameter is (String xpathExpression) , How do I change this to WebElement and use other element locators which will be defined in other pages. 因此,在以下方法中,参数为(String xpathExpression) ,如何将其更改为WebElement并使用将在其他页面中定义的其他元素定位符。

protected boolean CheckSorting(String xpathExpression) {
    List<WebElement> issueTypeDropdown = new LinkedList<>(driver.findElements(By.xpath(xpathExpression)));
    LinkedList<String> issueTypes = new LinkedList<String>();
    for (int i = 0; i < issueTypeDropdown.size(); i++) {
        //System.out.println(issueTypeDropdown.get(i).getText());
        issueTypes.add(issueTypeDropdown.get(i).getText());
    }
    return Compare(issueTypes);
}

You can't get the locator from WebElement . 您无法从WebElement获取定位器。 If you want the locator strategy to be dynamic you can send By to the method 如果您希望定位器策略是动态的,则可以将By发送给该方法

protected boolean CheckSorting(By by) {
    List<WebElement> issueTypeDropdown = new LinkedList<>(driver.findElements(by));
    //...
}

Uses: 用途:

CheckSorting(By.xpath(xpathExpression));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM