为了账号安全,请及时绑定邮箱和手机立即绑定

如何从硒的下拉列表中选择选项

如何从硒的下拉列表中选择选项

小怪兽爱吃肉 2023-07-19 16:28:02
如何在 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 类没有效果。当我单击列表时可见,但我无法找到该元素。硒看不到它。
查看完整描述

3 回答

?
天涯尽头无女友

TA贡献1831条经验 获得超9个赞

Work order number从dropdown“诱导”WebDriverWait和“跟随”xpath 选项中进行选择elementToBeClickable。


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


查看完整回答
反对 回复 2023-07-19
?
繁星淼淼

TA贡献1775条经验 获得超11个赞

使用以下代码:


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

    }

}


查看完整回答
反对 回复 2023-07-19
?
Smart猫小萌

TA贡献1911条经验 获得超7个赞

使用下面的代码。


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

        }

    }


查看完整回答
反对 回复 2023-07-19
  • 3 回答
  • 0 关注
  • 115 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信