0

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

1 Answer 1

2

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));
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks you!! But right now for my use, I have defined my element locator as public @FindBy(xpath = "(//INPUT[@type='search'])[4]/following-sibling::UL") WebElement dropdown_issueType; in the same page where I am going to use this method . Now how do I use this?
@moh17 You can't extract the locator from the Webelement. If you can't use the page factory to locate the list you need to pass string or By as parameter.
Thanks got it. But it would be helpful if you can guide me on how to use page factory to locate the list.
@moh17 Like you located single element, just to list public @FindBy(xpath = "xpath") List<WebElement> elements;
Will try this. Can I initiate a chat with you if i need any clarification?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.