Reputation: 139
As title says. It's easy to send keys to WebElement, since it has such method, but how to send them to an element of Select class (from package org.openqa.selenium.support.ui.Select).
Upvotes: 1
Views: 3518
Reputation: 6064
Write this code, it would work for you
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);
driver.get("http://www.seleniumeasy.com/test/jquery-dropdown-search-demo.html");
driver.findElement(By.cssSelector("span[aria-labelledby='select2-country-container']")).click();
driver.findElement(By.cssSelector("input.select2-search__field:nth-of-type(1)")).sendKeys("Australia");
driver.findElement(By.xpath("//li[text()='Australia']")).click();
Upvotes: 1
Reputation: 296
Try the below
driver.get("http://www.seleniumeasy.com/test/jquery-dropdown-search-demo.html");
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div[1]/div/div[2]/span/span[1]/span")).click();
driver.findElement(By.xpath("/html/body/span/span/span[1]/input")).sendKeys("India");
Thread.sleep(2000);
WebDriverWait wait = new WebDriverWait(driver, 30);
By option = By.xpath("//li[contains(text(),'India')]");
wait.until(ExpectedConditions.elementToBeClickable(option));
driver.findElement(option).click();
Please note that the xpath used here are not well formed. Kindly change that accordingly
Upvotes: 1