【问题标题】:Select options from Autopopulate text boxes using Selenium webdriver使用 Selenium webdriver 从自动填充文本框中选择选项
【发布时间】:2017-02-14 12:00:10
【问题描述】:
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);

您能帮我从下拉列表中选择自动填充值吗:

  1. 我们有客户端 textbox,这是一个自动填充框。
  2. 当我在客户端字段中输入“ho”文本时,它会显示下拉列表,其中包含与我输入的文本相关的值,即ho,然后我必须选择列表下可用的那些值。
  3. 在上面的代码中,我尝试了按 Enter,但无法选择值。

您能检查一下上面的代码并帮助我吗?

【问题讨论】:

  • 你能分享网站的网址或相关的HTML吗??....
  • 打包ChromeBrowser; public static void main(String[] args) { 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 。进入); }} 请检查上面的代码。
  • 但是我们可以在哪里检查您的代码。??
  • Sourabh,对不起,这是我公司的网站,我不能分享证书,我可以分享具体信息吗?
  • URL - bioceptbetaweb.azurewebsites.net 用户名 - ajay.kumar@technossus.com PWD- Ajay@123 登录后,请点击“下新订单”,然后在客户字段中,我们必须从自动填充弹出窗口。请检查

标签: java selenium xpath selenium-webdriver auto-populate


【解决方案1】:

你应该尝试如下:-

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();

【讨论】:

  • 感谢您的代码。它工作正常,但现在我在下一个字段中尝试了同样的事情并且无法找到该元素。在上面的代码中,您选择了您从代码中选择的“CSS Selector”,对吧?
  • @ajaykumar 是的,可能有一些不同的选择器会起作用,尝试识别定位器..:)
  • 我尝试过使用 ID 和 CSS 路径,但它不起作用。
  • @ajaykumar 那么你可以问另一个关于这个的问题..谢谢..:)
  • 谢谢,所以场景是: 1. 如果不选择客户端,我们无法选择设施作为其禁用。 2. 我们从下拉列表中选择了客户价值。 3. 现在我已经为新标签编写了一个脚本。 4. 之后,我要通过ID查找设施字段,但它显示元素未找到,那么请你帮我做同样的事情吗?
【解决方案2】:

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

// 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】:

试试这个:

Select drpCountry = new Select(driver.findElement(By.id("searchOptions")));
drpCountry.selectByVisibleText("By author");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-12
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多