2017-05-02 6 views
-1

Web Tableに 'Version'、 'Downloads'、 'Name'、 'Last Accessed on'の4つの列があります。セレンを使用してWebテーブルの列で最大値を見つける方法

ダウンロードの最大値(値)を持つバージョン番号をダウンロードしたい場合は、ダウンロード欄にダウンロードしてください。

セレンを使用してそれを達成する方法。

+0

[尋ねる]と[どの程度の研究努力が必要ですか?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow)をお読みください。 -users)試したコードとエラーメッセージなどの実行結果を提供してください。 – JeffC

答えて

0

まず、リスト内の要素を格納する必要があります。

List<WebElement> myList=driver.findElements(By.className("acc-toggle")); 
    //To store all web elements into list 

    List<String> all_elements_text=new ArrayList<>(); 
    //If you want to get all elements text into array list 

    for(int i=0; i<myList.size(); i++){ 

     all_elements_text.add(myList.get(i).getText()); 
     //loading text of each element in to array all_elements_text 
     System.out.println(myList.get(i).getText()); 
    } 

次のように次にあなたがコレクションAPIを使用して最大数を印刷することができます。

Object obj = Collections.max(all_elements_text); 

System.out.println(obj); 

は、それはあなたを助けることを願っています。

0

あなたは、以下のような何かを試すことができます。

static int max=0; 

static String version = null; 

List<WebElement> storeRows = storesTable.findElements(By.cssSelctor("table tr")); 

for(int i=0; i<storeRows.size();i++{ 

WebElement columnValue = storeRows.findElement(By.xpath("\td[3]")); 

WebElement versionElem = storeRows.findElement(By.xpath("\td[1]")); 

if(Integer.parseInt(columnValue.getText()) > max){ 

    version = versionElem .getText(); 
    max = Integer.parseInt(columnValue.getText()); 
    } 

} 

System.out.println("For Vrsion " + version + " are the highest downloads = " + max); 
0

まずあなたはバージョンを読む必要があるとそれに対応するダウンロードは、Webテーブルから数えます。以下のコードはそれを行います:

// Below line will get column headers and rows of webtable 
    List<WebElement> cols = driver.findElements(By 
      .xpath(".//table/thead/tr/td/div")); 
    List<WebElement> rows = driver.findElements(By 
      .xpath(".//table/tbody/tr")); 

    Map<String, Integer> records = new HashMap(); 

    // Add version column data into an array list 
    String version = ""; 
    int downloadCount = 0; 
    for (int rowCount = 0; rowCount < rows.size(); rowCount++) { 
     List<WebElement> rowCells = rows.get(rowCount).findElements(
       By.tagName("td")); 
     for (int count = 0; count < cols.size(); count++) { 
      // Get version into list 
      if (cols.get(count).getText().contains("Version")) { 
       version = rowCells.get(count) 
         .findElement(By.tagName("div")).getText(); 
      } 

      // Get downloads into list 
      if (cols.get(count).getText().contains("Downloads")) { 
       downloadCount = Integer.parseInt(rowCells.get(count) 
         .findElement(By.tagName("div")).getText()); 
      } 

      records.put(version, downloadCount); 
     } 
    } 

次に、最大ダウンロード数を確認する必要があります。ストリームの概念を使用して、あなたは以下のようにすることをを取得することができます:

// This will get max download value 
    Optional<Entry<String, Integer>> str2 = versions3.entrySet().stream() 
      .max(new Comparator<Entry<String, Integer>>() { 

       @Override 
       public int compare(Entry<String, Integer> o1, 
         Entry<String, Integer> o2) { 
        if (o1.getValue() > o2.getValue()) { 
         return 0; 
        } else { 
         return -1; 
        } 
       } 
      }); 
    // Max download value 
    System.out.println(str2.get().getValue()); 
    // Corresponding versionid 
    System.out.println(str2.get().getKey()); 

を同じダウンロード数(最大値)を持つ複数のレコードがある場合は、コードの下にすべてのバージョンを取得しますと仮定します。 versionidを取得するために文字列を操作する必要があります。

//If there are more than one version with same downlaod value (Max) then below code will fetch all versions 
    List<Object> str4 = versions3.entrySet().stream() 
      .filter(entry -> entry.getValue() == str2.get().getValue()) 
      .collect(Collectors.toList()); 
    System.out.println(str4); 

これが役に立ちます。

関連する問題