getText()和getFirstSelectedOption()。getText()没有给出预期的文本

时间:2019-04-22 07:59:28

标签: selenium-chromedriver

我试图显示下拉菜单的所有选项,然后选择一个选项

public class Toolsqa_SelectMenu {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("https://demoqa.com/selectmenu/");
        WebElement e = driver.findElement(By.xpath("//select[@id='speed']"));
        Select s = new Select(e);
        System.out.println("First selected item : " + s.getFirstSelectedOption().getText());

        List<WebElement> options = s.getOptions(); 

        for(WebElement w : options)     
        {
            System.out.println(" Items in drop down : " + w.getText());
        }

        s.selectByVisibleText("Faster");
    }

}

我得到了

First selected item : 
 Items in drop down : 
 Items in drop down : 
 Items in drop down : 
 Items in drop down : 
 Items in drop down : 
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible: Element is not currently visible and may not be manipulated
  (Session info: chrome=73.0.3683.103)
  (Driver info: chromedriver=2.39.562718 (9a2698cba08cf5a471a29d30c8b3e12becabb0e9),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 1.13 seconds
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:48:19 -0700'
System info: host: 'ADMIN-PC', ip: '192.168.43.181', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_25'
Driver info: org.openqa.selenium.chrome.ChromeDriver

预期:

First selected item : Medium
 Items in drop down : Slower
 Items in drop down : Slow
 Items in drop down : Medium
 Items in drop down : Fast
 Items in drop down : Faster

0 个答案:

没有答案