问题
Driver.findElement(By.xpath("//*[@id='client']")).sendKeys("Ho");
Driver.manage().timeouts().implicitlyWait(1,TimeUnit.MINUTES);
WebElement dropdown = (new WebDriverWait(Driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='client']")));
Driver.findElement(By.xpath("//*[@id='collapseClientInfo']/div/form/div[3]/div[2]/ul/li[1]/a")).sendKeys(Keys.ENTER);
Could you please help me to select auto populate value from drop down list:
- We've Client
textbox
which is an auto-populate box. - When I enter "ho" text in the client field, it shows me the drop down which has values related to my entered text i.e.
ho
, then I have to select those values which are available under list. - In above code I've tried with Press Enter but unable to select the value.
Could you please check the above code and help me out for the same?
回答1:
You should try as below :-
WebDriverWait wait = new WebDriverWait(Driver, 60);
//wait until loader invisible
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("loaderDiv")));
//this sleep is required because after invisibility of loader focus goes to first input which is Requisition Number
//If you are filling form from first input no need to for this sleep
//if you want to input directly to client field need to sleep to avoid focus first
Thread.sleep(3000);
//Now find the client input and set value
WebElement client = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("client")));
client.sendKeys("Ho");
//Now find all the showing option
List<WebElement> dropdownOptions = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("ul.dropdown-menu a")));
//Now select the first option
dropdownOptions.get(0).click();
回答2:
Below approach might be helpful:
// Enter text in auto complete text box
driver.findElement(By.xpath("//*[@id='client']")).sendKeys("Ho");
// Wait for options to display
Thread.sleep(5000);
// Option to select
String optionToSelect = "Honda";
Boolean isOptionSelected = Boolean.FALSE;
// Get the options displayed
List<WebElement> options = driver.findElements(By
.cssSelector("ul.dropdown-menu a"));
// Select option
for (WebElement webElement : options) {
if (webElement.getText().equalsIgnoreCase(optionToSelect)) {
webElement.click();
isOptionSelected = Boolean.TRUE;
}
}
if (isOptionSelected) {
// Option is selected
} else {
// Expected option is not displayed. Fail the script
}
回答3:
Try this: Select drpCountry = new Select(driver.findElement(By.id("searchOptions"))); drpCountry.selectByVisibleText("By author");
来源:https://stackoverflow.com/questions/42225629/select-options-from-autopopulate-text-boxes-using-selenium-webdriver