2017-04-21 76 views
-1

我有下面的代码验证下拉值

 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中的选项,它将获取我的选项值并与我的数组进行比较。任何人都可以帮我解决问题,我如何取文本而不是选项值。所以,我可以用我的文字排列

+0

能你也添加了相关的html代码? –

+0

尝试'getText()'而不是'getAttribute(“value”)' –

+0

可能的重复[如何使用selenium中的选择列表?](http://stackoverflow.com/questions/7695652/how-to-use- select-list-in-selenium) – JeffC

回答

0

文本比较也许为<option />属性value有该选项,而不是文本该选项的唯一代码(例如0)。

假设你的下拉代码有点像

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

您可能需要更改线路

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

String optionValue = allOptions.get(i).getText();

+0

非常感谢。它的工作现在很好。 – Meghasri