gpt4 book ai didi

java - 从
  • 下拉列表中选择值 - Selenium
  • 转载 作者:行者123 更新时间:2023-11-29 04:52:51 25 4
    gpt4 key购买 nike

    我试图从一个看起来像“ul”的小下拉列表中选择一个值这是我在 Firebug 中看到的:

        <button class="btn btn-default dropdown-toggle" aria-expanded="true"       aria-haspopup="true" data-toggle="dropdown" type="button">
    <ul class="dropdown-menu">
    <li>
    <a href="javascript:void(0)">1</a>
    </li>
    <li>
    <a href="javascript:void(0)">2</a>
    </li>
    <li>
    <a href="javascript:void(0)">3</a>

    这是我目前在 Java 中所拥有的:

     public void selectPhoneType(String option)
    {
    driver.findElement(By.className("caret")).click();
    new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.className("dropdown-menu")));
    WebElement element = driver.findElement()
    }

    假设我想选择/点击选项“1”。我将如何完成我的代码?我没有太多地使用“li”,所以我尝试使用一个 select,这显然没有用。任何帮助,将不胜感激!谢谢

    最佳答案

    尝试这样的事情:

    public void selectPhoneType(String option) {
    // Open the dropdown so the options are visible
    driver.findElement(By.className("dropdown-menu")).click();
    // Get all of the options
    List<WebElement> options = driver.findElements(By.xpath("//ul[@class='dropdown-menu']/li"));
    // Loop through the options and select the one that matches
    for (WebElement opt : options) {
    if (opt.getText().equals(option)) {
    opt.click();
    return;
    }
    }
    throw new NoSuchElementException("Can't find " + option + " in dropdown");
    }

    注意:Selenium 的 Select() 在这里不起作用,因为您的列表不在“选择”标签下。

    关于java - 从 <li> 下拉列表中选择值 - Selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34791599/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com