Skip to content

Could not select drop down list option in selenium java

Based on the image below the dropdown list option id is id=”CountryId” enter image description here

 Select dropdown = new Select(obj.findElement(By.id("CountryId")));
 System.out.println(dropdown + "is trigger");
 dropdown.selectByVisibleText("Malaysia");
 dropdown.selectByValue("52");

Based on the snippet above I am keep getting error which could not select the drop down list option

enter image description here

Answer

You can try with JS as well :

JavascriptExecutor js=(JavascriptExecutor) driver;
js.executeScript("return document.getElementById('CountryId').selectedIndex = '52';");

update 1 :

driver.manage().window().maximize();
driver.get("http://demowebshop.tricentis.com/cart");
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("BOOKS"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("h2.product-title a"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[id^='add-to-cart-button']"))).click(); 
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("shopping cart"))).click();
Select select  = new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("CountryId"))));
select.selectByValue("52");