を使用した動的 Web テーブルの処理 Selenium webdriver

の Web テーブル Selenium

Web 上で公開されている HTML テーブルには XNUMX 種類あります。

  1. 静的テーブル: データは静的です。つまり、行と列の数は固定されています。
  2. 動的テーブル: データは動的です。つまり、行と列の数は固定されていません。

で動的テーブルを処理する方法 Selenium

以下は、動的 Web テーブルの例です。 Selenium 販売用。入力日付フィルターに基づいて、行数が変更されます。つまり、本質的にはダイナミックなのです。

動的テーブルの処理 Selenium

静的テーブルの処理は簡単ですが、動的テーブルの処理は Selenium 行と列が一定ではないため、少し難しいです。

X-Path を使用した Web テーブル要素の検索

Web 要素を見つける前に、まず理解しましょう。

Web要素とは何ですか?

ウェブ要素とは、テキストボックス、ドロップダウン、ラジオボタン、送信ボタンなどのHTML要素に他なりません。これらのHTML要素は、 start タグとで終わる end タグ。

例えば、

初めての HTML ドキュメント

検索する Web 要素の X パスを取得する手順。

ステップ1) Chrome で、次の場所に移動します。 https://demo.guru99.com/test/web-table-element.php

X-Path を使用した Web テーブル要素の検索

ステップ2) x-path を取得する Web 要素を右クリックします。この場合は、「Company」を右クリックして [検査] オプションを選択します。次の画面が表示されます。

X-Path を使用した Web テーブル要素の検索

ステップ3) 強調表示された Web 要素を右クリックし、[コピー] -> [X パスのコピー] オプションを選択します。

X-Path を使用した Web テーブル要素の検索

ステップ4) コピーした X パス「//*[@id=”leftcontainer”]/table/thead/tr/th [1]」を使用します。 Selenium WebDriver を使用して要素を見つけます。

例: 動的 WebTable から行数と列数を取得する

動的な Web テーブルの処理中、 Seleniumの行数と列数を予測することはできません。

使い方 Selenium Webドライバー、見つかります

  • Web テーブルの行数と列数 Selenium
  • X 行または Y 列のデータ。

以下は、Webテーブルを処理するために行と列の合計数を取得するプログラムです。 Selenium:

行数と列数を取得する

import java.text.ParseException;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Noofrowsandcols {
    
public static void main(String[] args) throws ParseException {
    	WebDriver wd;
	  System.setProperty("webdriver.chrome.driver","G://chromedriver.exe");
	  wd= new ChromeDriver();
        wd.get("https://demo.guru99.com/test/web-table-element.php");         
        //No.of Columns
        List <webelement> col = wd.findElements(By.xpath(".//*[@id=\"leftcontainer\"]/table/thead/tr/th"));
        System.out.println("No of cols are : " +col.size()); 
        //No.of rows 
        List <webelement> rows = wd.findElements(By.xpath(".//*[@id='leftcontainer']/table/tbody/tr/td[1]")); 
        System.out.println("No of rows are : " + rows.size());
        wd.close();
    }
}

コードの説明:

  • ここでは、最初に Web ドライバー オブジェクト「wd」を宣言し、それを Chrome ドライバーに初期化しました。
  • リストを使用します「col」内の列の合計数に。
  • findElements コマンドは、指定されたロケーターに一致するすべての要素のリストを返します。
  • findElements と X-path //*[@id=\”leftcontainer\”]/table/thead/tr/th を使用して、すべての列を取得します
  • 同様に、行に対してこのプロセスを繰り返します。

出力:

行数と列数を取得する

例: 動的テーブルの特定の行と列のセル値をフェッチする

3 つ必要だと仮定しましょうrd テーブルの行とその XNUMX 番目のセルのデータ。 以下の表を参照してください。

特定の行と列のセル値を取得する

上の表では、データは一定期間後に定期的に更新されます。 取得しようとするデータは、上のスクリーンショットとは異なります。 ただし、コードは同じままです。 3を取得するサンプルプログラムは次のとおりです。rd 行と2nd 列のデータ。

特定の行と列のセル値を取得する

import java.text.ParseException;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;


public class RowandCell {
    public static void main(String[] args) throws ParseException {
    	WebDriver wd;
		System.setProperty("webdriver.chrome.driver","G://chromedriver.exe");
		 wd= new ChromeDriver();
		 wd.get("https://demo.guru99.com/test/web-table-element.php"); 
		 wd.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		 WebElement baseTable = wd.findElement(By.tagName("table"));
		  
		 //To find third row of table
		 WebElement tableRow = baseTable.findElement(By.xpath("//*[@id=\"leftcontainer\"]/table/tbody/tr[3]"));
         String rowtext = tableRow.getText();
		 System.out.println("Third row of table : "+rowtext);
		    
		    //to get 3rd row's 2nd column data
		    WebElement cellIneed = tableRow.findElement(By.xpath("//*[@id=\"leftcontainer\"]/table/tbody/tr[3]/td[2]"));
		    String valueIneed = cellIneed.getText();
		    System.out.println("Cell value is : " + valueIneed); 
		    wd.close();
    }
}

コードの説明:

  • テーブルは、ロケーター プロパティ「tagname」を使用して検索されます。
  • 使い方 XPath 「//*[@id=\”leftcontainer\”]/table/tbody/tr[3]」で 3 を見つけますrd getText() 関数を使用して行のテキストを取得します
  • Xpath「//*[@id=\”leftcontainer\”]/table/tbody/tr[3]/td[2]」を使用して、2 の 3 番目のセルを見つけます。rd getText() 関数を使用して行のテキストを取得します

出力:

動的テーブルの特定の行と列のセル値をフェッチする

例: 動的テーブルの列内のすべての値の最大値を取得する

この例では、特定の列のすべての値の最大値を取得します。

次の表を参照してください –

動的テーブルの列内のすべての値の最大値を取得する

これがコードです

動的テーブルの列内のすべての値の最大値を取得する

import java.text.ParseException;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.text.NumberFormat;


public class MaxFromTable {
    public static void main(String[] args) throws ParseException {
    	WebDriver wd;
		System.setProperty("webdriver.chrome.driver","G://chromedriver.exe");
		 wd= new ChromeDriver();
		 wd.get("https://demo.guru99.com/test/web-table-element.php"); 
		 String max;
	     double m=0,r=0;
		 
	       //No. of Columns
	        List <webelement> col = wd.findElements(By.xpath(".//*[@id='leftcontainer']/table/thead/tr/th"));
	        System.out.println("Total No of columns are : " +col.size());
	        //No.of rows
	        List <webelement> rows = wd.findElements(By.xpath (".//*[@id='leftcontainer']/table/tbody/tr/td[1]"));
	        System.out.println("Total No of rows are : " + rows.size());
	        for (int i =1;i<rows.size();i++)
	        {    
	            max= wd.findElement(By.xpath("html/body/div[1]/div[5]/table/tbody/tr[" + (i+1)+ "]/td[4]")).getText();
	            NumberFormat f =NumberFormat.getNumberInstance(); 
	            Number num = f.parse(max);
	            max = num.toString();
	            m = Double.parseDouble(max);
	            if(m>r)
	             {    
	                r=m;
	             }
	        }
	        System.out.println("Maximum current price is : "+ r);
    }
}

</webelement></webelement>

コードの説明:

  • Chrome ドライバーを使用して Web テーブルを見つけ、XPath「.//*[@id='leftcontainer']/table/tbody/tr/td[1]」を使用して行の総数を取得します。
  • for ループを使用して、合計行数を反復処理し、値を 1 つずつフェッチします。 次の行を取得するには、XPath で (i+XNUMX) を使用します。
  • 古い値と新しい値を比較し、最大値が for ループの最後に出力されます。

出力

動的テーブルの列内のすべての値の最大値を取得する

例: 動的テーブルのすべての値を取得する

次の表を検討してください。 https://demo.guru99.com/test/table.html

動的テーブルのすべての値を取得する

各行の列数は異なります。

ここでは、行番号 1、2、および 4 には 3 つのセルがあり、行番号 3 には 2 つのセルがあり、行番号 5 には 1 つのセルがあります。

すべてのセルの値を取得する必要があります

ここでは、コードは次のとおりです。

動的テーブルのすべての値を取得する

import java.text.ParseException;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeDriver;



public class NofRowsColmns {
    public static void main(String[] args) throws ParseException {
    	WebDriver wd;
    	System.setProperty("webdriver.chrome.driver","G://chromedriver.exe");
    	wd = new ChromeDriver();
    	wd.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    	wd.get("https://demo.guru99.com/test/table.html");
    	//To locate table.
    	WebElement mytable = wd.findElement(By.xpath("/html/body/table/tbody"));
    	//To locate rows of table. 
    	List < WebElement > rows_table = mytable.findElements(By.tagName("tr"));
    	//To calculate no of rows In table.
    	int rows_count = rows_table.size();
    	//Loop will execute till the last row of table.
    	for (int row = 0; row < rows_count; row++) {
    	    //To locate columns(cells) of that specific row.
    	    List < WebElement > Columns_row = rows_table.get(row).findElements(By.tagName("td"));
    	    //To calculate no of columns (cells). In that specific row.
    	    int columns_count = Columns_row.size();
    	    System.out.println("Number of cells In Row " + row + " are " + columns_count);
    	    //Loop will execute till the last cell of that specific row.
    	    for (int column = 0; column < columns_count; column++) {
    	        // To retrieve text from that specific cell.
    	        String celtext = Columns_row.get(column).getText();
    	        System.out.println("Cell Value of row number " + row + " and column number " + column + " Is " + celtext);
    	    }
    	    System.out.println("-------------------------------------------------- ");
    	}
   	}
}

コードの説明:

  • rows_count は行の合計数を示します。
  • 各行について、次を使用して列の合計数を取得します。 rows_table.get(row).findElements(By.tagName("td"));
  • 各列と各行を反復処理して値をフェッチします。

出力:

動的テーブルのすべての値を取得する

まとめ

  • By.xpath() は、テーブル要素にアクセスするためによく使用されます。
  • 静的 Web テーブル Selenium 本質的には一貫しています。つまり、固定数の行とセルデータがあります。
  • 動的 Web テーブルには一貫性がありません。つまり、固定数の行とセルのデータがありません。
  • Selenium Web ドライバーを使用すると、動的な Web テーブルを簡単に処理できます。
  • Selenium Webdriver を使用すると、動的 Web テーブルにアクセスできます。 Xパス

続きを読む readmore