Drop down menu 从selenium webdriver中的“引导下拉列表”中选择值?

Drop down menu 从selenium webdriver中的“引导下拉列表”中选择值?,drop-down-menu,selenium-webdriver,Drop Down Menu,Selenium Webdriver,工具:SeleniumWebDriver 我试图单击下拉列表中的特定值。我可以通过以下方式进行同样的操作: HTML代码段:通过选择属性 <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 cl

工具:SeleniumWebDriver

我试图单击下拉列表中的特定值。我可以通过以下方式进行同样的操作:

HTML代码段:通过选择属性

<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>
 @FindBy(xpath = ".//*[@id='organization_chosen']/a/span")
   public static WebElement organizationExpand;
 new Select(organizationExpand).selectByVisibleText("abc");
但是,如果代码具有Select属性,则上述选项1代码有效

如果开发人员使用这种方法对下拉列表进行编码,该怎么办

HTML代码片段:通过引导下拉方法:

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

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

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

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

我已经在TestComplete中实现了类似的解决方案。当然可以为你做。试试下面的方法

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

有关有用的选择选择器api的信息,请参阅以下基于@Ranjit Kancharla的想法并使用PhantomJS web驱动程序的作品:

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

id—所选元素的id;value-从option元素中选择的值

从html代码片段中可以看到,您可以通过单击倒三角形直接打开基础下拉列表。然后通过直接点击xpath来识别li元素。