0

I have a very similar problem to this one: Selenium: find element "next to" other element

My element looks like this:

<div class="a-Trinity has-left-widget has-right-widget" style="cursor: pointer;">
<div class="trinity-left-widget" style="">
    <div>
        <span class="layer-style-rule-slice" style="background-color: rgb(233, 34, 91); border-color: rgb(255, 255, 255); margin-top: 12px;"></span>
        <span class="layer-style-rule-slice" style="background-color: rgb(75, 170, 234); border-color: rgb(255, 255, 255); margin-top: 6px;"></span>
        <span class="layer-style-rule-slice" style="background-color: rgb(42, 200, 159); border-color: rgb(255, 255, 255); margin-top: 0px;"></span>
        <span class="layer-style-rule-slice" style="background-color: rgb(153, 207, 34); border-color: rgb(255, 255, 255); margin-top: -6px;"></span>
        <span class="layer-style-rule-slice" style="background-color: rgb(247, 151, 29); border-color: rgb(255, 255, 255); margin-top: -12px;"></span>
    </div>
</div>  
<div class="gwt-Label trinity-middle-widget form-text">Street Lights</div>  
<div class="trinity-right-widget" style="">
    <div class="small-switch-container layer-right-switch">
        <div class="small-switch switch-active"> 
            <div class="small-switch-button"></div> 
        </div>
    </div>
</div>

I want to check whether switch-active is shown or not. I am using the following to find the correct element:

    String className = "gwt-Label trinity-middle-widget form-text";
    String htmlElement = "div";
    String textToFind = layerName; // i.e. Street Light

    List<WebElement> elements = Common.findElementsUsingHtmlXpathClass(driver, htmlElement, className);

    Common.myPrint(thisClass + " elements count: " + elements.size());
    for (WebElement element : elements) {
        // select an element
        String text = Common.getAllAttributes(element, driver);
        if (text != null) {
            text = text.trim();
            if (text.contains(textToFind)) {
                // so this is my element
                // here is where I need to get the next one

            }
        }
    }

The code for findElementsUsingHtmlXpathClass:

public static List<WebElement> findElementsUsingHtmlXpathClass(WebDriver driver, String htmlElement,
        String className) {
    List<WebElement> elements = driver
            .findElements(By.xpath("//" + htmlElement + "[contains(@class, '" + className + "')]"));
    return elements;
}

I cannot see how to apply the solution given in the article quoted, as I do not have an element I can just find by ID.

How can I get this next element?

Went with this solution from cruisepandey. I merely changed the name of the method to reflect it's new function:

    public static List<WebElement> findElementsFollowingHtmlXpathClass(WebDriver driver, String htmlElement,
        String className) {
    List<WebElement> elements = driver
            .findElements(By.xpath("//" + htmlElement + "[contains(@class, '" + className + "')]/following-sibling::div[@class='trinity-right-widget']/descendant::div[contains(@class,'switch-active')]"));

    return elements;
}
5
  • try this String cssSelector = "gwt-Label.trinity-middle-widget.form-text"; Commented May 18, 2018 at 15:40
  • @cruisepandey - I think you have misunderstood the question. I already have that element! Commented May 18, 2018 at 15:41
  • Do you actually want to find if the next element contains the class switch-active? Commented May 18, 2018 at 15:56
  • 1
    I think the two answers below contain enough for you to solve this. Commented May 18, 2018 at 15:57
  • this question does not deserver a down vote without explaining comment . I’m up voting it. Commented May 19, 2018 at 17:44

1 Answer 1

1

You can use it like this :

public static List<WebElement> findElementsUsingHtmlXpathClass(WebDriver driver, String htmlElement,
        String className) {
    List<WebElement> elements = driver
            .findElements(By.xpath("//" + htmlElement + "[contains(@class, '" + className + "')]/following-sibling::div[@class='trinity-right-widget']/descendant::div[contains(@class,'switch-active')]"));
    return elements;
}
Sign up to request clarification or add additional context in comments.

2 Comments

that does not check the innerHtml - which must contain the text Street Lights.
actually, that one worked when I called it in place of the original Common.findElementsUsingHtmlXpathClass. Took me a moment to work out how!

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.