简体   繁体   中英

How can I loop through list of WebElements and select one WebElement with a condition?

I am using Selenium WebDriver with Java. I am trying to create a method that loops through a list of WebElements to return the first WebElement that contains the text "Ooga Booga", or return null if there are no elements in the list that contains this text.

public class myClass {
 private static WebElement element = null;
 private static List<WebElement> elements = null;

 public static WebElement returnOneElement (WebDriver driver){
    elements = driver.findElements(By.cssSelector("someSelector"));
    for (WebElement element : elements){
        String myText = element.findElement(By.cssSelector("classC")).getText();
        if (myText.contains("Ooga Booga")) {
            return element;
        }
    }
    return null;
}

This all works fine until I run into an element that does not have this attribute:

String myText = element.findElement(By.cssSelector("classC")).getText();

How can I continue to loop through my List while ignoring any elements that do not have "someDiv" inside of it?

<div class="someSelector">
    <div class="classA">
    <div class="classB">
</div>

<div class="someSelector">
    <div class="classA">
    <div class="classB">
    <div class="classC">
</div>

I need to search for text inside div "classC".

Use findElement-S method as shown below.

    for (WebElement element : elements) {
        List<WebElement> mightHaveSomeDiv = element.findElements(By.cssSelector("someDiv"));

        if (mightHaveSomeDiv.size() > 0) {
            //Can iterate the list if you expect more than one div of type someDiv.
            String myText = mightHaveSomeDiv.get(0).getText();
            if (myText.contains("Ooga Booga")) {
                return element;
            }
        }
    }

Here is your answer.. Firstly Declare a parent web locator

WebElement element1 = driver.findElement(By.id("##abc##"));

Now in case you want to perform any action or anything within the locator defined above then you can do it as follows:

element1.findElement(By.xpath("  .        //##yourFurtherLocABC")).click();

element1.findElement(By.xpath("  .      //##yourFurtherLocXYZ")).getText();

Important = Do remember to keep a . at the beginning of any child elements!

Btw, I would recommend to try and break away from static methods like this. It will make your life a living hell later on because of it being static. Everything using it starts to want static.

Anyway, lets break it down. This cssSelector will allow you to grab the child divs by the tagname instead of classname.

By.cssSelector(".someSelector div")

Now that we have a list of WebElement, which are all div elements, lets just look for the text in the div. No need to worry about class attribute

public static WebElement returnOneElement (WebDriver driver){
            elements = driver.findElements(By.cssSelector(".someSelector div"));
            for (WebElement element : elements){
                if(element.getText().contains("Ooga Booga")) {
                    return element;
                }
            }    
       return null;
    }

Alternatively, you may find the target directly with xpath locators such as

//div[@class='someSelector']//div[contains(.,'Ooga Booga')]

Be warned though, if you have multiple div that contains the text "Ooga Booga", you will get multiple WebElements, and what you do with it next may not be what you expect. Actually, this warning should also apply to the loop. It will break out of your first div element that contains "Ooga Booga". You will have to adjust the logic if you want exact match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM