3
votes

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");
2
Share you dropdown HTML as well?? try to print the option size as System.out.println(LineOfBusiness .getOptions().size()); and let us know the result..:)Saurabh Gaur
<select id="lineOfBusiness" class="w420p h40p bdrno" onchange="ShowHideETE(-1);" name="lineOfBusiness"> <option value="">Select line of business</option> <option value="1">HWW Sales</option> <option value="2">EAN Sales</option> <option value="4">HWW Email</option> <option value="5">A2A Email</option> <option value="6">A2A Phone</option> <option value="7">CDS Email</option> <option value="8">CDS Phone</option> <option value="9">A2A Follow-up Team</option> <option value="10">CDS Follow-up Team</option> </select>Pardeep Kochhar
What is the output for this System.out.println(LineOfBusiness.getOptions().size());??Saurabh Gaur
Did you tried to select option using visible text as LineOfBusiness.selectByVisibleText("HWW Sales");??Saurabh Gaur
I used System.out.println(LineOfBusiness .getOptions().size()); It shows the size in Console 10 but with same errorPardeep Kochhar

2 Answers

0
votes

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");
0
votes
 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);
    }
}