Selenium选择以字符串形式传递的变量的下拉选项

时间:2012-07-20 15:27:56

标签: java drop-down-menu selenium

背景:

  • 卡片库存 - 包含程序的详细信息,库存位置和库存国家/地区
  • 客户销售卡片屏幕 - 当您向客户出售新卡片时,您必须输入他们的地址。一旦你进入他们的国家,特定领域(州/地址第2行/邮政编码)要么成为强制性的,要么是自愿的。

问题是正在使用的两个国家/地区数据库不同且可能不同。 “德国”显示在卡片股票和“德国,联邦共和国”的卖卡屏幕上

我的流程:

1)搜索发行前卡以从卡片库中获取国家/地区 - 将此变量分配给字符串,即aString

2)出售该卡

3)在国家/地区下拉框中 - 如果aString位于该列表中,请选择aString,如果没有,则创建“其他人”列表以捕捉变体

我的代码一直告诉我该字符串不在列表中,countrydropdown打印为false,即使我在两个国家/地区都进行了测试

非常感谢任何帮助

Boolean countrydropdown = "xpath=//select[@id='address.country']/option]".indexOf(aString) > 0;
System.out.println("countrydropdown");
System.out.println(countrydropdown);


<tr>
    <td class="labelFormReq">*</td>
    <td class="labelForm">Country:</td>
    <td>
        <select id="address.country" onchange="validateAndSubmit(this, 'selectCountryEvent');" name="address.country">
            <option value="">Please Select</option>
            <option value="4">Afghanistan</option>
            <option value="248">Alan Islands </option>
            <option value="8">Albania</option>
            <option value="12">Algeria</option>
            <option value="16">American Samoa</option>
            <option value="20">Andorra</option>
            <option value="24">Angola</option>
            <option value="660">Anguilla</option>
            <option value="10">Antarctica</option>
            <option value="28">Antigua and Barbuda</option>
            <option value="32">Argentina</option>
            <option value="51">Armenia</option>
            <option value="533">Aruba</option>
            <option value="36">Australia</option>
        </select>
    </td>
</tr>

1 个答案:

答案 0 :(得分:0)

Boolean countrydropdown = "xpath=//select[@id='address.country']/option]".indexOf(aString) > 0;

并不真正搜索元素。实际上,它会在文本aString中查找"xpath=//select[@id='address.country']/option]"。为了使它返回任何有用的东西,你必须用方法调用来包装它。见:

Boolean countrydropdown = selenium.isElementPresent("xpath=//select[@id='address.country']/option[text()='" + aString + "']");

使其更具可读性和约定性:

boolean countryDropdown = selenium.isElementPresent("xpath=id('address.country')/option[text()='" + aString + "']");

当且仅当true元素的<option>子项存在且文本等于您的adress.country时,才会返回aString

或者,如果你想减少oneliney:

boolean countryDropdown = false;

String[] countryOptions = selenium.getSelectOptions("id=address.country");
for (String option : countryOptions) {
    if (option.equals(aString)) {
        countryDropdown = true;
        break;
    }
}