工具:Selenium Webdriver
我试图点击下拉列表中的特定值。我可以通过以下方式做同样的事情:
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>
自动化选项1:通过“选择”属性
@FindBy(xpath = ".//*[@id='organization_chosen']/a/span")
public static WebElement organizationExpand;
new Select(organizationExpand).selectByVisibleText("abc");
但是,如果代码具有“选择”属性,则上述“选项1”代码有效。
如果开发人员使用“bootstrap dropdown”方法编码下拉菜单会怎么样?
HTML片段:通过“bootstrap dropdown”方法:
<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>
答案 0 :(得分:1)
以下是我为bootstrap下拉菜单找到的解决方案:
@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
}
答案 1 :(得分:1)
您可以在页面上评估JavaScript,这意味着您也可以在页面上运行jQuery。
我已在测试完成中实施了类似的解决方案。当然可以为你完成。尝试类似下面的内容。
selenium.getEval("var window = this.browserbot.getUserWindow();window.jQuery('#organizationType_chosen').selectpicker('val', '1')");
有关有用的选择选择器api
,请参阅http://silviomoreto.github.io/bootstrap-select/答案 2 :(得分:0)
以下也适用(基于@Ranjit Kancharla的想法,使用PhantomJS网络驱动程序):
org.openqa.selenium.support.events.EventFiringWebDriver driver = ...
driver.executeScript("$('#" + id +"').selectpicker('val', '" + value + "');");
id - “select”元素的ID; 值 - “option”元素中的选定值