moh17
moh17

Reputation: 243

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.

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.

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);
}

Upvotes: 0

Views: 3047

Answers (1)

Guy
Guy

Reputation: 50809

You can't get the locator from WebElement. If you want the locator strategy to be dynamic you can send By to the method

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

Uses:

CheckSorting(By.xpath(xpathExpression));

Upvotes: 2

Related Questions