验证下拉值

时间:2017-04-21 14:31:55

标签: selenium-webdriver drop-down-menu

我有以下代码

        String[] expected = {"Cancellation Context", "Cancellation Refund Type", "Claim Approval Letter Trigger", "Claim Cost Status", "Fronting  Insurer", "Minutes Of Delay For Automated Claim Email", "Payment Terms", "Payment Type","Product Offering","Quote Origin","Rate Override","Rating Point","Renewal Type","Second Payment From","Segment is required (th-TH)","Segments","Subsequent Payment Duration Type"};

        WebElement select = driver.findElement(AdminScreens.cmb_GlobalDDList);
        List<WebElement> allOptions = select.findElements(By.xpath(".//option"));

        int A1=expected.length;
        int A2=allOptions.size()-1;

        System.out.println("A1 and A1 are "+A1+ " and " +A2 );

        // make sure you found the right number of elements
        if (A1 != A2) {
            System.out.println("fail, wrong number of elements found");
        }
        // make sure that the value of every <option> element equals the expected value
        for (int i = 0; i < A1; i++) {
            String optionValue = allOptions.get(i).getAttribute("value");
            System.out.println("optionValues are "+optionValue);
            System.out.println("expected[i] are "+expected[i]);

            if (optionValue.equals(expected[i])) {
                System.out.println("passed on: " + optionValue);
            } else {
                System.out.println("failed on: " + optionValue);
            }
        }

当我运行代码时,我的代码失败并显示结果集如下

A1 and A1 are 17 and 17
optionValues are 0
expected[i] are Cancellation Context
failed on: 0
optionValues are 42
expected[i] are Cancellation Refund Type
failed on: 42
optionValues are 39
expected[i] are Claim Approval Letter Trigger
failed on: 39
optionValues are 28
expected[i] are Claim Cost Status
failed on: 28
optionValues are 25
expected[i] are Fronting  Insurer
failed on: 25
optionValues are 40
expected[i] are Minutes Of Delay For Automated Claim Email
failed on: 40
optionValues are 29
expected[i] are Payment Terms
failed on: 29
optionValues are 14
expected[i] are Payment Type
failed on: 14
optionValues are 24
expected[i] are Product Offering
failed on: 24
optionValues are 11
expected[i] are Quote Origin
failed on: 11
optionValues are 43
expected[i] are Rate Override
failed on: 43
optionValues are 15
expected[i] are Rating Point
failed on: 15
optionValues are 20
expected[i] are Renewal Type
failed on: 20
optionValues are 41
expected[i] are Second Payment From
failed on: 41
optionValues are 26
expected[i] are Segment is required (th-TH)
failed on: 26
optionValues are 44
expected[i] are Segments
failed on: 44
optionValues are 38
expected[i] are Subsequent Payment Duration Type
failed on: 38

我怀疑代码行

List<WebElement> allOptions = select.findElements(By.xpath(".//option"));
我使用的

是错误的...因为我在xpath中选择了获取我的选项值并与我的数组进行比较。任何人都可以帮助我如何采取文本而不是选项值。这样我就可以将文本与我的数组文本进行比较

1 个答案:

答案 0 :(得分:0)

value的{​​{1}}属性可能包含该选项的唯一代码(例如<option />),而不是该选项的文本。

假设您的下拉代码有点像

0

您可能需要更改行

<option value="0">Cancellation Context</option>

String optionValue = allOptions.get(i).getAttribute("value");