ChromeDriver无法访问选择

时间:2012-01-20 15:48:17

标签: java select selenium webdriver

我正在尝试强制ChromeDriver从选择框中选择一个项目。然而它的失败就行了

    Options = select.getOptions(); 

奇怪的是,在下面的代码中,getText();方法打印出选择中的所有项目:

  productChoooser.getText() = ...
  BASE 2013
  BASE 2014

但我仍然遇到异常......试图点击选择框中的项目的代码:

    List<WebElement> Options;
    WebElement productChoooser = driver.findElement(By.id("mainForm:aucPanelId:1:product"));
    System.out.println("productChoooser.getText() = " + productChoooser.getText());
    Select select = new Select(productChoooser);
    Options = select.getOptions();
    String product = "BASE 2013";
    for (WebElement option:Options){
        System.out.println("option.getText() = " + option.getText()); 
       if(option.getText().equals(product)){
           option.click();
       }
    }

有谁知道如何避免它?我尝试将Chrome和chromedriver.exe更新到最新版本,但没有成功......

堆栈追踪:

  

org.openqa.selenium.WebDriverException :(警告:服务器未提供任何堆栈跟踪信息)   命令持续时间或超时:47毫秒   构建信息:版本:'2.16.1',修订版:'15405',时间:'2012-01-05 12:30:12'   系统信息:os.name:'Windows XP',os.arch:'x86',os.version:'5.1',java.version:'1.6.0_20'   驱动程序信息:driver.version:RemoteWebDriver       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)       at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)       at java.lang.reflect.Constructor.newInstance(Constructor.java:513)       在org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:147)       在org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:113)       在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:435)       在org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:231)       在org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:161)       在org.openqa.selenium.remote.RemoteWebElement.findElementsByTagName(RemoteWebElement.java:227)       在org.openqa.selenium.By $ ByTagName.findElements(By.java:313)       在org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:146)       在org.openqa.selenium.support.ui.Select.getOptions(Select.java:70)       在com.deutscheboerse.testing.RegulationTest.selectProduct(RegulationTest.java:53)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)       在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:44)       在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)       在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)       在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)       在org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)       在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:193)       在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:52)       在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)       在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:42)       在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:184)       在org.junit.runners.ParentRunner.run(ParentRunner.java:236)       在org.junit.runner.JUnitCore.run(JUnitCore.java:157)       at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)       在com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)       在com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)       在com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

1 个答案:

答案 0 :(得分:1)

        WebElement select = webDriver.findElement(selector);
        List<WebElement> optionList = select.findElements(By.tagName("option"));
        for (WebElement option : optionList) {
            if (option.getText().trim().equalsIgnoreCase("your value")) {
                option.click();
                break;
            }
        }