简体   繁体   English

硒清单 <WebElement> 总是返回null

[英]Selenium List <WebElement> always returns null

Selenium List<WebElement> returns zero when getoption is used to retrieve values from a drop-down menu. 当使用getoption从下拉菜单中检索值时,Selenium List<WebElement>返回零。

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 ================================================ ====================我使用了getOptions()但是它也不起作用

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 . 总是返回ONE元素,因为只有一个ID为month元素。 This does return the options inside of it (use getOptions method) 这确实返回其中的选项(使用getOptions方法)

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). 我所做的另WebDriverWait更改是使用WebDriverWait ,使其具有明确的等待条件(直到网页上显示月份下拉菜单),检查给定的持续时间(20秒)。 If the element found in first second itself, the element will be returned, it won't wait till 20 seconds. 如果该元素在第一秒内被发现,该元素将被返回,它不会等到20秒。 Similarly, if the element is not found after 20 seconds, Timeout exception will be thrown. 同样,如果20秒后未找到该元素,则将抛出Timeout异常。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium WebDriver:List中每个WebElement中的findElement() <WebElement> 总是返回第一个元素的内容 - Selenium WebDriver: findElement() in each WebElement from List<WebElement> always returns contents of first element 为什么选择WebElement和列表 <WebElement> 通过xpath以不同的方式处理,并且List始终为null? - Why WebElement and List<WebElement> are processed not in the similar way via xpath and List is always null? Selenium中的“ StaleElementReferenceException”用于列表<WebElement> - “StaleElementReferenceException” in Selenium for a List<WebElement> Selenium webdriver 将 webelement 存储在 webelement 列表中 - Selenium webdriver store webelement in webelement list @FindBy查找webelement返回null - @FindBy finding webelement returns null 列表<WebElement>返回空列表 - List<WebElement> returns empty List Selenium WebDriver 无耻地返回错误的 WebElement 高度 - Selenium WebDriver Shamelessly Returns Incorrect Height of WebElement Webelement 使用 selenium webdriver 获取空值 - Webelement get null values using selenium webdriver 无法单击硒列表Webelement中的第二个Webelement(anchor标签) - Not able to click on the 2nd webelement(anchor tag) in a list webelement in selenium @FindBy批注定义的Webelement返回空指针 - Webelement defined by @FindBy annotation returns null pointer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM