Java 如何在 Selenium 2 中选择/获取下拉选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6430462/
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-08-16 05:51:30  来源:igfitidea点击:

How to select/get drop down option in Selenium 2

javaseleniumdrop-down-menuselenium-webdriver

提问by user786045

I am converting my selenium 1 code to selenium 2 and can't find any easy way to select a label in a drop down menu or get the selected value of a drop down. Do you know how to do that in Selenium 2?

我正在将我的 selenium 1 代码转换为 selenium 2,但找不到任何简单的方法来在下拉菜单中选择标签或获取下拉菜单的选定值。你知道如何在 Selenium 2 中做到这一点吗?

Here are two statements that work in Selenium 1 but not in 2:

以下是在 Selenium 1 中有效但在 2 中无效的两个语句:

browser.select("//path_to_drop_down", "Value1");
browser.getSelectedValue("//path_to_drop_down");

采纳答案by janderssn

Take a look at the section about filling in formsusing webdriver in the selenium documentation and the javadoc for the Selectclass.

查看selenium 文档和Select类的 javadoc 中有关使用 webdriver填写表单的部分。

To select an option based on the label:

要根据标签选择选项:

Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down")));
select.deselectAll();
select.selectByVisibleText("Value1");

To get the first selected value:

要获取第一个选定的值:

WebElement option = select.getFirstSelectedOption()

回答by Kartmcad

This is the code to select value from the drop down

这是从下拉列表中选择值的代码

The value for selectlocator will be the xpath or name of dropdown box, and for optionLocator will have the value to be selected from the dropdown box.

selectlocator 的值将是下拉框的 xpath 或名称,而 optionLocator 的值将是从下拉框中选择的值。

public static boolean select(final String selectLocator,
        final String optionLocator) {
    try {
        element(selectLocator).clear();
        element(selectLocator).sendKeys(Keys.PAGE_UP);
        for (int k = 0; k <= new Select(element(selectLocator))
                .getOptions().size() - 1; k++) {
            combo1.add(element(selectLocator).getValue());
            element(selectLocator).sendKeys(Keys.ARROW_DOWN);
        }
        if (combo1.contains(optionLocator)) {
            element(selectLocator).clear();
            new Select(element(selectLocator)).selectByValue(optionLocator);
            combocheck = element(selectLocator).getValue();
            combo = "";

            return true;
        } else {
            element(selectLocator).clear();
            combo = "The Value " + optionLocator
                    + " Does Not Exist In The Combobox";
            return false;
        }
    } catch (Exception e) {
        e.printStackTrace();
        errorcontrol.add(e.getMessage());
        return false;
    }
}



private static RenderedWebElement element(final String locator) {
    try {

        return (RenderedWebElement) drivers.findElement(by(locator));
    } catch (Exception e) {
        errorcontrol.add(e.getMessage());
        return (RenderedWebElement) drivers.findElement(by(locator));
    }
}

Thanks,

谢谢,

Rekha.

雷卡。

回答by AlekseiPetrovski

in ruby for constantly using, add follow:

在 ruby​​ 中不断使用,添加以下内容:

module Selenium
  module WebDriver
    class Element
      def select(value)
        self.find_elements(:tag_name => "option").find do |option|
          if option.text == value
            option.click
              return
           end
       end
    end
  end
end

and you will be able to select value:

您将能够选择值:

browser.find_element(:xpath, ".//xpath").select("Value")

回答by coolcub

Try using:

尝试使用:

selenium.select("id=items","label=engineering")

or

或者

selenium.select("id=items","index=3")

回答by thrasher

driver.findElement(By.id("id_dropdown_menu")).click();
driver.findElement(By.xpath("xpath_from_seleniumIDE")).click();

good luck

祝你好运

回答by Ben

A similar option to what was posted above by janderson would be so simply use the .GetAttribute method in selenium 2. Using this, you can grab any item that has a specific value or label that you are looking for. This can be used to determine if an element has a label, style, value, etc. A common way to do this is to loop through the items in the drop down until you find the one that you want and select it. In C#

与 janderson 上面发布的内容类似的选项是简单地使用 selenium 2 中的 .GetAttribute 方法。使用它,您可以获取任何具有特定值或标签的项目。这可用于确定元素是否具有标签、样式、值等。执行此操作的常用方法是遍历下拉列表中的项目,直到找到所需的项目并选择它。在 C# 中

int items = driver.FindElement(By.XPath("//path_to_drop_Down")).Count(); 
for(int i = 1; i <= items; i++)
{
    string value = driver.FindElement(By.XPath("//path_to_drop_Down/option["+i+"]")).GetAttribute("Value1");
    if(value.Conatains("Label_I_am_Looking_for"))
    {
        driver.FindElement(By.XPath("//path_to_drop_Down/option["+i+"]")).Click(); 
        //Clicked on the index of the that has your label / value
    }
}

回答by Praveen

you can do like this :

你可以这样做:

public void selectDropDownValue(String ValueToSelect) 
{

    webelement findDropDownValue=driver.findElements(By.id("id1"))    //this will find that dropdown 

    wait.until(ExpectedConditions.visibilityOf(findDropDownValue));    // wait till that dropdown appear

    super.highlightElement(findDropDownValue);   // highlight that dropdown     

    new Select(findDropDownValue).selectByValue(ValueToSelect);    //select that option which u had passed as argument
}

回答by Jophin P John

This method will return the selected value for the drop down,

此方法将返回下拉列表的选定值,

public static String getSelected_visibleText(WebDriver driver, String elementType, String value)
  {
    WebElement element = Webelement_Finder.webElement_Finder(driver, elementType, value);
   Select Selector = new Select(element);
    Selector.getFirstSelectedOption();
    String textval=Selector.getFirstSelectedOption().getText();
    return textval;
  }

Meanwhile

同时

String textval=Selector.getFirstSelectedOption();

String textval=Selector.getFirstSelectedOption();

element.getText();

element.getText();

Will return all the elements in the drop down.

将返回下拉列表中的所有元素。