简体   繁体   中英

Selenium List <WebElement> always returns null

Selenium List<WebElement> returns zero when getoption is used to retrieve values from a drop-down menu.

Code snippet:

public class FaceBookdropDownMenu {
    public static void main(String[] args) throws InterruptedException {
        System.getProperty("webdriver.gecko.driver", "//usr//local//bin//geckodriver 6");
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("https://www.facebook.com/");

        WebElement month_dropdown = driver.findElement(By.id("month"));
        //return a list of month names
        System.out.println(month_dropdown.getText());
        List<WebElement> month_lists = driver.findElements(By.id("month"));   
        int total_month= month_lists.size();
        // returns 1 instead of 12 
        System.out.println("Total month count is"+ total_month);

        for(WebElement ele:month_lists) {
            String month_name = ele.getText();
            System.out.println("Months are:"+ month_name); 
        }
    }
}

====================================================================== I use the getOptions() but it does not work either

WebElement month_dropdown =driver.findElement(By.id("month"));
System.out.println(month_dropdown.getText());
Select month_dd = new Select(month_dropdown);
List <WebElement> month_lists = month_dd.getOptions();       
int total_month= month_lists.size();
//Zero is returned instead of 12
System.out.println("Total month count is"+ total_month);

for(WebElement ele:month_lists) {
    String month_name = ele.getText();
    System.out.println("Months are:"+ month_name);
}

Following code worked for me:

import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class FacebookDateSelect {

    public static void main(String[] args) {


        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.facebook.com/");
        driver.manage().window().maximize();

        WebDriverWait wait = new WebDriverWait(driver, 20);
        WebElement month_dropdown = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("month")));
        Select month_dd = new Select(month_dropdown);
        List <WebElement> month_lists = month_dd.getOptions();       
        int total_month= month_lists.size();
        System.out.println("Total month count is"+ total_month);

        for(WebElement ele:month_lists) {
            String month_name = ele.getText();
            System.out.println("Months are:"+ month_name);
        }

        //updated code - to select random option using Random class
        month_dd.selectByIndex(new Random().nextInt(user_country.getOptions().size()));
        driver.quit();

    }

}

In your code,

List<WebElement> month_lists = driver.findElements(By.id("month"));

always returns ONE element as there is only one element with id month . This does return the options inside of it (use getOptions method)

The other change I made is to use WebDriverWait , to have explicit wait condition (until month dropdown is displayed on the web page), check for the given duration (20 seconds). If the element found in first second itself, the element will be returned, it won't wait till 20 seconds. Similarly, if the element is not found after 20 seconds, Timeout exception will be thrown.

Output I got:

Starting ChromeDriver 2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed) on port 6720
Only local connections are allowed.
Total month count is13
Months are:Month
Months are:Jan
Months are:Feb
Months are:Mar
Months are:Apr
Months are:May
Months are:Jun
Months are:Jul
Months are:Aug
Months are:Sept
Months are:Oct
Months are:Nov
Months are:Dec

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