0

I am testing a web application using Java and Selenium. I am trying to click a button on a page where two buttons exist with the same classname and text. So I find the parent element first, and then look for my button among its child elements. I am executing the following and getting unexpected results.

public static List<WebElement> findSubElementsUsingHtmlXpathClass(String htmlElement,
        String className, WebElement parent) {
    String xPathString="//" + htmlElement + "[contains(@class, '" + className + "')]";

    List<WebElement> elements = parent.findElements(By.xpath(xPathString));
    return elements;
}

This is returning elements that do not belong to the parent element.

This is where I am calling it from:

String htmlElement = "div";
    String className = "tabs-container";
    List<WebElement> elements = Common.findElementsUsingHtmlXpathClass(driver, htmlElement, className);
    Common.myPrint(thisClass + " no of elements found: " + elements.size());
    for (WebElement element : elements) {
        // outerHTML: <input class="form-control btn btn-info" value="Create item"
        // type="button">
        // inner:
        String htmlElement2 = "input";
        String className2 = "form-control btn btn-info";
        String textToFind = "Create item";
        List<WebElement> subElements = Common.findSubElementsUsingHtmlXpathClass(htmlElement2, className2,
                element);HTML

Am I missing something?

I have been able to work around this, by simply ignoring the first elemnt it returns, but this can only be guaranteed in this specific case.

3
  • I am not 100% sure I understand the use case, but this looks like a very roundabout way of doing it. Why would you not just use a CSS-selector that only selects the one button you want? It could look something like this: #parentId .class-of-button Commented Jun 20, 2018 at 13:22
  • @Metareven CSS-selectors have proved of little use in the system I am testing, as there are no permanent structures. The site uses Google Web Toolkit, so all the ids and CSS addresses are changeable. Commented Jun 20, 2018 at 13:26
  • To quote a meme then: "Jesus christ how horrifying". Anyway, that can't be completely true, or else your example would always fail as you are reliant on having elements with the fixed classnames you have in your example (tabs-container, form-control, btn, btn-info). I can't see anything in your example that couldn't be done with a css-selector. Commented Jun 20, 2018 at 13:33

2 Answers 2

2

Your XPath needs to be changed as below (. needs to be added before the double slash).you need to select the child element from the current parent element .So, in Xpath, . needs to be specified to indicate it as current node.

public static List<WebElement> findSubElementsUsingHtmlXpathClass(String htmlElement,
        String className, WebElement parent) {
    String xPathString=".//" + htmlElement + "[contains(@class, '" + className + "')]";

    List<WebElement> elements = parent.findElements(By.xpath(xPathString));
    return elements;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try using CSS-Selector instead of XPath:

public static List<WebElement> findSubElementsUsingCSS(String className, WebElement parent) {
        String cssString="." + className;

        List<WebElement> elements = parent.findElements(By.cssSelector(cssString));
        return elements;
    }

4 Comments

that did not work. The CSS Selector for this item (from Firefox) is .margin-top-10 > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > input:nth-child(1), but is subject to change from run to run.
But you can get the elements with class only using css selector [seleniumeasy.com/selenium-tutorials/…
@SteveStaple That CSS-selector is horrible and you should basically never use the autogenerated selectors you get from your browser. I strongly recommend reading up on CSS-selectors and how they work as this is usually the easiest way to select elements, as well as being easier to maintain and read compared to xpath-selectors, though xpath-selectors can be more expressive
thanks @Metareven, I already have an answer for this problem.

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.