【问题标题】:selecting value from "bootstrap dropdown" in selenium webdriver?从 selenium webdriver 中的“引导下拉菜单”中选择值?
【发布时间】:2014-04-15 13:29:52
【问题描述】:

工具:Selenium Webdriver

我正在尝试单击下拉列表中的特定值。我可以通过以下方式做同样的事情:

HTML sn-p:通过“选择”属性

<select class="regionpicker-state span3 ng-valid ng-dirty" ng-change="updateSelection()" ng-options="a.code as a.name for a in state" ng-model="selectedState">
  <option class="" value="">Select</option>
  <option value="0">Karnataka</option>
  <option value="1">Tamil Nadu</option>
</select>

自动化选项 1:通过“选择”属性

 @FindBy(xpath = ".//*[@id='organization_chosen']/a/span")
   public static WebElement organizationExpand;
 new Select(organizationExpand).selectByVisibleText("abc");

但是,如果代码具有“选择”属性,则上述“选项 1”代码有效。

如果开发人员使用“bootstrap dropdown”方法对下拉列表进行编码怎么办?

HTML sn-p:通过“引导下拉”方法:

<select id="organization" class="focused" data-placeholder="Choose a organization..." name="organization" style="display: none;">
   <div id="organization_chosen" class="chosen-container chosen-container-single chosen-container-active" style="width: 220px;" title="">
      <a class="chosen-single chosen-default" tabindex="-1">
      <div class="chosen-drop">
         <div class="chosen-search">
         <ul class="chosen-results">
           <li class="active-result" data-option-array-index="1" style="">Demo</li>
           <li class="active-result" data-option-array-index="2" style="">abc</li>
           <li class="active-result" data-option-array-index="3" style="">def</li>
           <li class="active-result" data-option-array-index="4" style="">xyz</li>
         </ul>

【问题讨论】:

  • 从我从html sn-p 中看到的,您可以通过单击倒三角形直接打开底层下拉列表。然后通过直接点击 xpath 来识别 li 元素。

标签: drop-down-menu selenium-webdriver


【解决方案1】:

这是我为引导下拉菜单找到的解决方案:

@FindBy(xpath =".//*[@id='organizationType_chosen']/a/span") //this xpath is referring the path where we can click on expanding the drop-down
    public static WebElement orgTypeExpand;
@FindBy(xpath =".//*[@id='organizationType_chosen']/div/ul") //this xpath is referring to exact location to the <ul> attribute which will be visible in HTML DOM structure only after you expand the drop-down
    public static WebElement orgTypeDropDown;

public static void organizationTypeBootStrapDropDownValueSelection(String organizationTypeValue)//consider parameter passed for 'organizationTypeValue' is 'abc' value from drop-down
{
    int flag=0;
    orgTypeExpand.click();
    //Do a implicit wait here until the dropdown menu expands as the HTML Dom structure dynamically gets updated with each dropdown values once the drop-down expands. 

    List<WebElement> organizationType = orgTypeDropDown.findElements(By.className("active-result")); //retrieve all values from the drop-down. Note: This pageobject 'orgTypeDropDown' has slight different xpath than 'orgTypeExpand'
    flag=0;
    for (WebElement orgType : organizationType) //For each value retrieved check if it matches with our expected value to be clicked 'organizationTypeValue'
    {   
        if (orgType.getText().equals(organizationTypeValue)) 
        {
            orgType.click();
            flag=1;
            break;
        }
    }
    Assert.assertTrue(flag==1, "OrganizationType '" + organizationTypeValue + "' passed as parameter is a mismatch with values available in organization Type drop-down...");//This will assist you in making sure if the drop-down value was clicked or not. It fails if the value is not selected & stops the script execution
}

【讨论】:

    【解决方案2】:

    你可以在页面上评估 JavaScript,这意味着你也可以在页面上运行 jQuery。

    我已经在测试完成中实施了类似的解决方案。当然可以为您完成。试试下面的东西。

    selenium.getEval("var window = this.browserbot.getUserWindow();window.jQuery('#organizationType_chosen').selectpicker('val', '1')");  
    

    参考http://silviomoreto.github.io/bootstrap-select/ 获取有用的选择选择器 api

    【讨论】:

      【解决方案3】:

      以下方法也有效(基于@Ranjit Kancharla 的想法,使用 PhantomJS 网络驱动程序):

      org.openqa.selenium.support.events.EventFiringWebDriver driver = ...
      driver.executeScript("$('#" + id +"').selectpicker('val', '" + value + "');");
      

      id - "select" 元素的 id;value - 从 "option" 元素中选择的值

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 2019-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多