java 如何从 Selenium webdriver 的“跨度类型下拉列表”中选择值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26689428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:23:53  来源:igfitidea点击:

how to select the value from "Span Type dropdown" in Selenium webdriver

javaseleniumselenium-webdriver

提问by Nagaraju Gampa

how to select the value from "Span Type dropdown" in Selenium webdriver

如何从 Selenium webdriver 的“跨度类型下拉列表”中选择值

I am able to click on dropdown by using XPath but not able to select the value from the dropdown. My XPath for clicking on Dropdown is:

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

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

When i use the above code in Selenium, dropdown is expanded but i am not able to select the value from the Dropdown

当我在 Selenium 中使用上面的代码时,下拉列表被展开但我无法从下拉列表中选择值

MY HTML code is as below:

我的 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>

How can I select the value from dropdown?

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

回答by Rishi Khanna

You can use the following function to select values from your drop-down
The below function will select 0 value from the drop-down, you can parameterize the following line (temp.equals("0") and pass the value which you want to select

您可以使用以下函数从下拉列表中选择值以下函数将从下拉列表
中选择 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;
        }
    }

回答by Zeeshan Siddiqui

I believe the application you are automating is using YUI Library.

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

Note in YUI Library clicking on every element with class containing 'yui-menu-button' will display the menu items of the dropdown. These menu items are wrapped in a DIV element containing class 'yui-menu-button-menu'.

请注意,在 YUI 库中,单击类包含“yui-menu-button”的每个元素将显示下拉菜单项。这些菜单项包含在包含类“yui-menu-button-menu”的 DIV 元素中。

The architecture of the application in your case is implementing a suffix. I believe and correct me if I'm wrong, the ID of all dropdowns on the page are in the format:

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

[dropdownName]Button& [dropdownName]SelectionMenu

[下拉名称]按钮[下拉名称]选择菜单

eg.

例如。

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

So the actual name/id of the dropdown is 'country'. In the above case it is 'minexp'. (Minimum Experience I think. Therefore DropdownID is 'minexp' and not 'minexpButton' or 'minexpSelectionMenu'. It may apply similarly to other elements in your application. Please conduct an architectural review of the application to get a better understanding of the element IDs and YUI library.

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

Here is how you can select from a YUI SelectMenu (dropdown):

以下是从 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;
                }
            }  
       }        
}

Tried and tested on the YUI Library for Dropdown.

用于 DropdownYUI 库上进行了尝试和测试。

I hope this helped. :)

我希望这有帮助。:)