【问题标题】:how to select the value from "Span Type dropdown" in Selenium webdriver如何从 Selenium webdriver 中的“跨度类型下拉列表”中选择值
【发布时间】:2014-12-28 15:28:11
【问题描述】:

如何从 Selenium webdriver 中的“Span Type 下拉菜单”中选择值

我可以使用 XPath 单击下拉列表,但无法从下拉列表中选择值。 我点击 Dropdown 的 XPath 是:

driver.findElement(By.xpath(".//*[@id='minexpButton']/span")).click();

当我在 Selenium 中使用上述代码时,下拉菜单会展开,但我无法从下拉菜单中选择值

我的 HTML 代码如下:

<span id="minexpButton" class="yui-button yui-menu-button yui-button-active yui-menu-button-active" style="background-color: rgb(255, 255, 255); display: -moz-inline-box;">

<div id="minexpSelectionMenu" class="yui-module yui-overlay yui-button-menu yui-menu-button-menu" style="z-index: 1003; visibility: visible; left: 367.683px; top: 1050.6px;">

<div class="bd">
<div class="selectionMenu">
<div class="ulDiv" style="overflow: auto; width: 64px; height: 210px;">
<div class="liDiv selected">

<a class="txt_black heading_4" href="#" tabindex="-1" target="_self">- Min -</a>
</div>

<div class="liDiv">
<a class="txt_black heading_4" href="#" tabindex="-1" target="_self">0</a>
</div>

<div class="liDiv">
<a class="txt_black heading_4" href="#" tabindex="-1" target="_self">1</a>
</div>

如何从下拉列表中选择值?

【问题讨论】:

  • 请添加用于从下拉列表中选择值的代码和 .//*[@id='minexpButton']/span --> 我没有看到作为子项的 span 标签带有 id = 'minexpButton' 的标签

标签: java selenium selenium-webdriver


【解决方案1】:

您可以使用以下函数从下拉列表中选择值
下面的函数将从下拉列表中选择 0 值,您可以参数化以下行 (temp.equals("0") 并传递您要选择的值

 List<WebElement> element = driver.findElements(By.cssSelector(".txt_black.heading_4"));
    for (int i = 0; i < element.size(); i++) {
        String temp = element.get(i).getText();
        if (temp.equals("0")) {
            element.get(i).click();             
            break;
        }
    }

【讨论】:

    【解决方案2】:

    我相信您正在自动化的应用程序正在使用 YUI 库。

    注意在 YUI 库中单击每个类包含 'yui-menu-button' 的元素将显示下拉菜单项。这些菜单项被包装在一个包含类 'yui-menu-button-menu' 的 DIV 元素中。

    在您的案例中,应用程序的架构正在实现一个后缀。如果我错了,我相信并纠正我,页面上所有下拉列表的 ID 格式为:

    [dropdownName]Button & [dropdownName]SelectionMenu

    例如。

    <span id="countryButton" class="...">
    </span>
    ....
    <div id="countrySelectionMenu" class="">
    ....
    </div>
    

    所以下拉菜单的实际名称/ID 是“国家/地区”。在上述情况下,它是“minexp”。 (我认为最低经验。因此 DropdownID 是“minexp”而不是“minexpButton”或“minexpSelectionMenu”。它可能同样适用于您的应用程序中的其他元素。请对应用程序进行架构审查以更好地理解元素 ID和 YUI 库。

    您可以通过以下方式从 YUI SelectMenu(下拉菜单)中进行选择:

    // Remember dropdownID is the 'minexp' and not 'minexpButton' or 'minexpSelectionMenu'    
    public void selectOption(String dropdownID, String optionText) {
           // Get the dropdown button
           WebElement dropdownButton = driver.findElement(By.id(dropdownID & "Button"));
    
           // Click on the dropdown button, this will make the selection menu visible
           dropdownButton.click();
    
           // Get the dropdown selection menu, since it is now visible you can select from it
           WebElement dropdownMenu = driver.findElement(By.id(dropdownID & "SelectionMenu"));
    
           // Verify selection menu is visible
           if(dropdownMenu.isDisplayed()) {
                 List<WebElement> menuItems = dropdownMenu.findElements(By.tagName("a"));
                 for(WebElement menuItem : menuItems) {
                    if(menuItem.getText().trim().toLowerCase().equalsIgnoreCase(optionText.trim().toLowerCase())) {
                           menuItem.click();
                           break;
                    }
                }  
           }        
    }
    

    YUI Library for Dropdown 上进行了尝试和测试。

    我希望这会有所帮助。 :)

    【讨论】:

      【解决方案3】:

      请尝试以下代码:

      driver.findElement(By.id(“dropdownField”)).sendKeys(“mention text of required 
       value from dropdown”); //send required option in dropdown field
      driver.findElement(By.id(“option1”)).click();
      

      或者试试下面的代码:

      List<WebElement> options = driver.findElements(By.xpath(“”));
      for(WebElement option : options) {
      if (option.getText().contains(“mention text of required value from dropdown”)) 
      {
       option.click();
       break;
      }
      

      请参阅link 了解更多详情 - 如何在不使用 Selenium 中 Select 类的方法的情况下选择下拉列表中的特定值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-08
        • 2019-07-02
        • 1970-01-01
        • 2018-06-14
        相关资源
        最近更新 更多