【问题标题】:How to select values in dropdownlist with selenium?如何使用硒在下拉列表中选择值?
【发布时间】:2013-07-07 13:45:45
【问题描述】:

我在 Eclipse 中使用 selenium、TestNG 和 java 来执行自动化测试。我在单击按钮(selenium.click(“button”),将值传递给文本框(selenium.type(“component”,“value”))和点击等命令方面取得了成功,但是当它附带组件类型下拉列表(与 common 或 asp.net MVC 相关)我无法使用命令 selenium.select ("field", "value") 选择字段。

要选择值甚至是字段,我是用XPath来的,但即便如此,用下拉列表还是不能,或者只能部分。

当下拉列表接受我输入的值时,我可以使用 selenium.click,但如果没有,到目前为止我尝试过的任何方法都不起作用。

【问题讨论】:

    标签: java selenium selenium-webdriver selenium-rc testng


    【解决方案1】:

    使用 webdriver 你可以用 Select 类来做我已经发布了一个在下面工作的代码 看看那个,Select Class 有 api 可以通过它的索引和值来选择下拉值,看看 Select api 了解更多信息

       public static void dropdown() 
        {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://demosthenes.info/blog/166/HTML-Forms-Drop-down-Menus");
        Select sele = new Select(driver.findElement(By.id("state")));
        sele.selectByIndex(1);
        }
    

    【讨论】:

    • 对不起,这行不通。 Select 类仅适用于带有
    • @Lucas 如果可能,请发布您的 html dom,以便我们也可以帮助您解决问题
    【解决方案2】:

    有几种方法可以从下拉列表中选择元素。以下是其中一些,您可以将它们保留为常见的下拉操作并调用您需要的任何方法。

    //select the dropdown using "select by visible text"
            public static void dropDownSelectByText(WebElement webElement, String VisibleText){
                Select selObj=new Select(webElement);
                selObj.selectByVisibleText(VisibleText);
            }
    
    //select the dropdown using "select by index"
    public static void dropDownSelectByIndex(WebElement webElement, int IndexValue){
        Select selObj=new Select(webElement);
        selObj.selectByIndex(IndexValue);
    }
    
    //select the dropdown using "select by value"
    public static void dropDownSelectByValue(WebElement webElement, String Value){
        Select selObj=new Select(webElement);
        selObj.selectByValue(Value);
    }
    

    您可以调用上述方法,如

    CommonPageOperations.dropDownSelectByValue(selectSubClientFromDropDownXpath,strSubClientName);
    

    只有当鼠标移动到特定位置时才会出现下拉列表,然后您还必须使用操作

    public void mouseMoveToExpandIcon(){
            Actions action = new Actions(driver);
            action.moveToElement(expandButtonXpath).perform();
        }
    

    【讨论】:

      【解决方案3】:
      WebElement select = driver.findElement(By.id("selection"));
      List<WebElement> options = select.findElements(By.tagName("option"));
      for (WebElement option : options) {
        if("Germany".equals(option.getText()))
          option.click();
      }
      

      【讨论】:

        【解决方案4】:
        Actions actions = new Actions(driver);
        WebElement dBox1= (new    WebDriverWait(driver,10)).until(ExpectedConditions.elementToBeClickable(By.id("selection""))).    selectByVisibleText("");
        actions.moveToElement(dBox1);
        actions.click();
        actions.perform();
        

        【讨论】:

          【解决方案5】:

          您必须在 selenium 中使用 Select 从下拉值中进行选择。

          //按ID

          WebDriver driver = new FirefoxDriver();
          new Select (driver.findElement(By.id("usState"))).selectByVisibleText("FL");
          

          //通过 XPath

          new Select (driver.findElement(By.xpath("xPath for dropdown"))).selectByVisibleText("FL");
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-01-11
            • 2021-12-13
            • 1970-01-01
            • 2015-12-31
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多