繁体   English   中英

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

[英]How to select/get drop down option in Selenium 2

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

以下是两个在Selenium 1中有效但在2中不起作用的语句:

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

查看有关使用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()
driver.findElement(By.id("id_dropdown_menu")).click();
driver.findElement(By.xpath("xpath_from_seleniumIDE")).click();

祝好运

在红宝石中不断使用,添加如下:

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")

尝试使用:

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

要么

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

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

你可以这样做:

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
}

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

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

element.getText();

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

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

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

谢谢,

雷卡。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM