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

发布于 2024-11-16 15:37:38 字数 276 浏览 1 评论 0原文

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

以下两条语句在 Selenium 1 中有效,但在 Selenium 2 中无效:

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

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?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

抚笙 2024-11-23 15:37:38

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

要根据标签选择选项:

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

要获取第一个选定的值:

WebElement option = select.getFirstSelectedOption()

Take a look at the section about filling in forms using webdriver in the selenium documentation and the javadoc for the Select class.

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()
简单气质女生网名 2024-11-23 15:37:38
driver.findElement(By.id("id_dropdown_menu")).click();
driver.findElement(By.xpath("xpath_from_seleniumIDE")).click();
driver.findElement(By.id("id_dropdown_menu")).click();
driver.findElement(By.xpath("xpath_from_seleniumIDE")).click();
以往的大感动 2024-11-23 15:37:38

在经常使用的 ruby​​ 中,添加 follow:

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

,您将能够选择值:

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

in ruby for constantly using, add follow:

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")
妖妓 2024-11-23 15:37:38

尝试使用:

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

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

Try using:

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

or

selenium.select("id=items","index=3")
甲如呢乙后呢 2024-11-23 15:37:38

与 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
    }
}

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#

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
    }
}
狼性发作 2024-11-23 15:37:38

你可以这样做:

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
}

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
}
分分钟 2024-11-23 15:37:38

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

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;
  }

同时

String textval=Selector.getFirstSelectedOption();

元素.getText();

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

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

element.getText();

Will return all the elements in the drop down.

看春风乍起 2024-11-23 15:37:38

Selenium WebDriver 中的选择

Selenium WebDriver 中的“Select”类用于选择和取消选择下拉列表中的选项。 Select 类型的对象可以通过将下拉 webElement 作为参数传递给其构造函数来初始化。

WebElement testDropDown = driver.findElement(By.id("testingDropdown"));
选择下拉菜单 = new Select(testDropDown);

从下拉列表中选择选项

从下拉列表中选择选项的方法有以下三种:

  1. selectByIndex – 根据索引选择选项,从 0 开始。

dropdown.selectByIndex(3);

  1. selectByValue – 根据选项的“值”属性选择选项。

dropdown.selectByValue("数据库");

  1. selectByVisibleText – 根据选项上的文本选择选项。

dropdown.selectByVisibleText("数据库测试");

Select in Selenium WebDriver

The ‘Select’ class in Selenium WebDriver is used for selecting and deselecting the option in a dropdown. The objects of Select type can be initialized by passing the dropdown webElement as parameter to its constructor.

WebElement testDropDown = driver.findElement(By.id("testingDropdown"));
Select dropdown = new Select(testDropDown);

Selecting options from dropdown

There are three ways of selecting options from dropdown-

  1. selectByIndex – To select an option based on its index, beginning with 0.

dropdown.selectByIndex(3);

  1. selectByValue – To select an option based on its ‘value’ attribute.

dropdown.selectByValue("Database");

  1. selectByVisibleText – To select an option based on the text over the option.

dropdown.selectByVisibleText("Database Testing");

泛滥成性 2024-11-23 15:37:38

使用 Selenium 中 Select 类的方法在下拉列表中选择特定值
Select 类实现 select 标签,提供帮助方法来选择和取消选择选项。

WebElement dropdownlist = driver.findElement(By.xpath(locator));
Select listbox = new Select(dropdownlist);

select 类方法的使用及示例:

selectByIndex(int index): Select the option at the given index.
listbox.selectByIndex(2);

selectByVisibleText(java.lang.String text): Select all options that display text matching the argument
listbox.selectByVisibleText(“Date”);

更详细的讨论请参考以下链接 这里

Select a particular value in a dropdown using the methods of Select class in Selenium
Select class implement select tag, providing helper methods to select and deselect options.

WebElement dropdownlist = driver.findElement(By.xpath(locator));
Select listbox = new Select(dropdownlist);

Use of methods of select class with examples:

selectByIndex(int index): Select the option at the given index.
listbox.selectByIndex(2);

selectByVisibleText(java.lang.String text): Select all options that display text matching the argument
listbox.selectByVisibleText(“Date”);

Please refer to the below link for more detailed discussions here

傲鸠 2024-11-23 15:37:38

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

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

谢谢,

雷卡。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文