从自动建议下拉列表中选择元素的方法有哪些?

时间:2018-03-07 06:50:54

标签: java selenium-webdriver

  1. 使用sendkeys键入输入字段。
  2. 自动提示来了。 html元素如下:

    <div angucomplete-alt id="ac-{{row.RowId}}" placeholder="Search materials..." maxlength="50" pause="100" selected-object="selectedCon" ng-click="selectedConRow(row)" local-data="materialsConsumables" search-fields="MaterialName" title-field="MaterialName" initial-value="row.materialSelected" minlength="1" input-class="autocomplete" match-class="highlight"></div>

  3. 将所有建议收集到“列表”中,并在匹配时选择其中一个元素。

  4. 选择值并将其填充到输入字段
  5. 填写所有其他必填字段
  6. 点击“提交”。但错误显示此输入字段为空,尽管它具有值。 (P.S。:手动它可以工作,应用程序本身没有问题。)
  7. 代码::

    String producttoSelect = "0007950137 - BSS 500ML GLASS -CDN";
    
    WebElement ConProduct1 = objDriver.findElement(By.xpath("//*[@class=\"con_Material ng-isolate-scope\"]/div/input"));
        ConProduct1.sendKeys("0007950137 - BSS 500ML GLASS -CDN");
    
        try {
                        Thread.sleep(5000);
        } catch (Exception e) {
        }
    
        List<WebElement> productList = objDriver.findElements(By.xpath("//*[@class=\"con_Material ng-isolate-scope\"]/div/div"));
        for (WebElement optionP : productList) {
                        System.out.println(optionP.getText());
                        if (optionP.getText().equals(producttoSelect)) {
                                        System.out.println("Trying to select Product: " + optionP.getText());
                                        optionP.click();
                                        break;
                        }
        }
    

1 个答案:

答案 0 :(得分:0)

而不是thread.sleep,尝试使用Explicit wait直到填充列表:

By aaa = By.xpath("//*[@class=\"con_Material ng-isolate-scope\"]/div/div");

        List<WebElement> suggestList = 
                wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(aaa));
相关问题