Steve Staple
Steve Staple

Reputation: 3279

Java Selenium - Find next element

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

Upvotes: 0

Views: 1437

Answers (1)

cruisepandey
cruisepandey

Reputation: 29362

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

Upvotes: 1

Related Questions