将jar文件更新到selenium 3.0后无法选择下拉选项

时间:2016-08-30 09:57:26

标签: java selenium selenium-webdriver

我在更新新的selenium 3.0 beta jar文件后使用以下代码。然而,在早期版本的硒中它完美地运作。

我更新了错误信息:

  

org.openqa.selenium.NoSuchElementException:找不到索引的选项:1

     

有关此错误的文档,请访问:http://seleniumhq.org/exceptions/no_such_element.html   构建信息:版本:'未知',修订版:' 2aa21c1',时间:' 2016-08-02 14:59:43 -0700'   系统信息:主持人:' GUR-ITD-8C',ip:' 172.21.45.117',os.name:' Windows 7',os.arch: ' amd64',os.version:' 6.1',java.version:' 1.8.0_31'   驱动程序信息:driver.version:未知

expedia.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);   
Select LineOfBusiness = new Select(expedia.findElement(By.id("lineOfBusiness")));
LineOfBusiness.selectByIndex(1);
Assert.assertEquals(LineOfBusiness.getFirstSelectedOption().getText(), "HWW Sales");

2 个答案:

答案 0 :(得分:0)

我不确定您使用Select类的问题是什么,但如果您想要一个替代解决方案来摆脱此错误,您可以尝试使用JavascriptExecutor,如下所示: -

public static String selectByIndex(WebDriver driver, WebElement select, int index) {

        String javaScript = "var select = arguments[0]; "
                + "select.options[arguments[1]].selected = true;"
                + "return select.selectedOptions[0].text";

        return (String) ((JavascriptExecutor) driver).executeScript(javaScript, select, index);   
}


WebElement select = expedia.findElement(By.id("lineOfBusiness"));

Assert.assertEquals(selectByIndex(expedia, select, 1), "HWW Sales");

答案 1 :(得分:0)

 WebElement select = driver.findElement(By.xpath(".//* 
 [@id='App']/div/div[3]/div/div/div[2]/div/form/div[2]/div[2]/div/select"));

 List<WebElement> allOptions = select.findElements(By.tagName("option"));

for (WebElement option : allOptions)
{
    if (examboard.equals("Select") && option.getText().equals("Select") )
    {
        new Select(By.xpath("//select").findElement(driver)).selectByVisibleText(allOptions.get(0).getText());
        System.out.println("Name of the exam board is -->" + examboard);
        Utils.waitForElement(1000);
    }
}