首页 > 解决方案 > 无法在我的控制台中打印下拉选项文本

问题描述

    WebElement AllDeptDropDown = driver.findElement(By.cssSelector("#search-in-dept"));

    Select s = new Select(AllDeptDropDown); 

    List<WebElement>DrpDownOption = s.getOptions();
    System.out.println("Total All Dropdown otions are - " + DrpDownOption.size());
    for (WebElement i : DrpDownOption) {
        System.out.println("DorpdownOptions Texts are - " +i.getText());

    }

控制台输出:

Total All Dropdown otions are - 30
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
DorpdownOptions Texts are - 
PASSED: ShopSite

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

标签: javaselenium

解决方案


如果您想在下拉列表中打印所有选项,那么您可能想使用此代码

            Select s =  new Select(Webelement of select tag);
            List<WebElement> allOptions = driver.findElements(By.xpath("xpath for all options may be a class name"));
            for(WebElement options : allOptions){
                System.out.println(options.getText());
    }

推荐阅读