Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

java - unable to select dropdown option after updating jar files to selenium 3.0

I am using the following code after updating the new selenium 3.0 beta jar files. However in earlier version of selenium it was working perfectly.

I updated as it was giving error :

org.openqa.selenium.NoSuchElementException: Cannot locate option with index: 1

For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: 'unknown', revision: '2aa21c1', time: '2016-08-02 14:59:43 -0700' System info: host: '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 info: driver.version: unknown

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");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm not sure what is the issue in your case using Select class, but if you want an alternate solution to get rid from this error, you can try using JavascriptExecutor as below :-

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");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...