2017-04-21 74 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