Webdriver从自动填充文本框中选择选项

Webdriver从自动填充文本框中选择选项

本文介绍了使用Selenium Webdriver从自动填充文本框中选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:

  1. 我们有一个客户端textbox,它是一个自动填充的框.
  2. 当我在客户字段中输入"ho"文本时,会显示一个下拉列表,其中包含与我输入的文本相关的值,即ho,然后我必须选择列表下可用的那些值.
  3. 在上面的代码中,我尝试按Enter键,但无法选择该值.
  1. We've Client textbox which is an auto-populate box.
  2. 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.
  3. 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?

推荐答案

以下方法可能会有所帮助:

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
}

这篇关于使用Selenium Webdriver从自动填充文本框中选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 08:01