【问题标题】:How to choose option from dropdown list in selenium如何从硒的下拉列表中选择选项
【发布时间】:2020-01-11 06:12:23
【问题描述】:

如何在 Selenium 中单击每个选项。

<div class="el-select-dropdown__wrap el-scrollbar__wrap" style="margin-bottom: -17px; margin-right: -17px;" xpath="1">
  <ul class="el-scrollbar__view el-select-dropdown__list">
  <!---->
    <li class="el-select-dropdown__item selected hover" style="">
      <span>Part number</span>
    </li>
    <li class="el-select-dropdown__item">
      <span>Work order number</span>
    </li>
  </ul>
</div>

我尝试了 Actions 类,Select 类没有效果。 虽然我单击的列表是可见的,但我无法找到该元素。 Selenium 没有看到它。

【问题讨论】:

    • Part number
    • 工单编号
  • 这是结构

标签: java selenium


【解决方案1】:

dropdown 中选择Work order number 诱导WebDriverWaitelementToBeClickable 以及以下xpath 选项。

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='el-select-dropdown__wrap el-scrollbar__wrap']/ul[@class='el-scrollbar__view el-select-dropdown__list']//li[./span[text()='Work order number']]")));
element.click()

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='el-select-dropdown__wrap el-scrollbar__wrap']/ul[@class='el-scrollbar__view el-select-dropdown__list']//li//span[text()='Work order number']")));
element.click()

【讨论】:

    【解决方案2】:

    使用以下代码:

    WebDriverWait wait = new WebDriverWait(driver, 30);
    WebElement dropdown = driver.findElement(By.xpath(".//ul[starts-with(@class,'el-scrollbar__view')]"));    
    List<WebElement> options = driver.findElements(By.xpath(".//li[starts-with(@class,'el-select-dropdown__item')]"));
    
    public void selectOption(String option){
        wait.until(ExpectedConditions.elementToBeClickable(dropdown));
        dropdown.click();
        wait.until(ExpectedConditions.visibilityOfAllElements(options));
        for(WebElement element : options){
             if(element.getText().equals(option))
                  element.click();
        }
    }
    

    希望它对你有用。

    【讨论】:

    • 请将注释从代码块中取出,并为代码添加一些解释以及它如何回答问题。
    • 使用显式等待等待元素可点击和可见。调用方法 selectOption("零件号")。它将单击下拉列表并比较传递的字符串,如果匹配,它将从列表中选择该选项。
    【解决方案3】:

    使用下面的代码。

        WebDriverWait wait = new WebDriverWait(driver, 30);
        WebElement dropdown = driver.findElement(By.xpath(".//ul[starts-with(@class,'el-scrollbar__view')]"));
        List<WebElement> options = driver.findElements(By.xpath(".//li[starts-with(@class,'el-select-dropdown__item')]"));
    
        @Test
        public void testCase1() {
            wait.until(ExpectedConditions.elementToBeClickable(dropdown));
            dropdown.click();
            wait.until(ExpectedConditions.visibilityOfAllElements(options));
            for (WebElement element : options) {
                element.click();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 2017-05-19
      相关资源
      最近更新 更多