2016-08-29 13 views
1

私はJavaとSeleniumを使ってテストを行っています。私のターゲットアプリケーションのDOMには、私が通過する必要がある多くのテーブルがあります。私は次のようなものを使用するために使用される:テーブルの行と列を扱う方法

WebElement mytable = driver.findElement(By.xpath(".//table/tbody")); 
    List<WebElement> rows_table = mytable.findElements(By.tagName("tr")); 
    int rows_count = rows_table.size(); 
    for (int row=0; row<rows_count; row++){ 
    List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td")); 
    int columns_count = Columns_row.size(); 
    System.out.println("Number of cells In Row "+row+" are "+columns_count); 
    for (int column=0; column<columns_count; column++){ 
    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("--------------------------------------------------"); 
    } 

しかし、私は簡単な方法でテーブルを扱うことができる何かを探していたので、私は検索し、見つかった:

1)selenium.getTable

2)GetTable(ElementFinder finder, JavascriptLibrary js)

しかし、私はそれらのための良いコードのサンプルを見つけることができませんでした。

短くて短い話.//trまたは.//tdをテーブル内の行と列を扱うよりも良い方法があるのだろうかと思っていましたか?

+0

ドキュメントごとに、getTable(tableLocator.row.column)を使用してセルの内容を取得できます。ここで、行と列はゼロに基づいています。 – lsiva

+0

@ Lsivaありがとう、すべてのコードとインポートの回答として追加できますか? –

答えて

1

getTableがwebdriverに実装されていません。しかし、あなたはこれを試すことができます。

WebDriver driver = new ChromeDriver(); 
Selenium selenium = new WebDriverBackedSelenium(driver, "your website url"); 
selenium .getTable("xpath"); 

selenium.getTable( "table.1.2"); getTable(tableCellAddress)引数:

tableCellAddress - a cell address, e.g. "foo.1.4" 

Returns: the text from the specified cell 

Gets the text from a cell of a table. The cellAddress syntax tableLocator.row.column, where row and column start at 0. 

方法についてを抑圧警告を追加します。

+0

ありがとう、そこからセレンのオブジェクトを取得する必要がありますか? descreaptionも追加できますか? –

+0

私は 'DefaultSelenium selenium = ...'を使うとDefaultSeleniumに黒い線が現れます –

+0

私はそれをチェックしましたDefaultSeleniumは償却されました。 – ChanChow

0

同様の質問の答えは、次のスレッドに答えた:実際に、さらに(getTableHeader、などの方法で、表のユーティリティクラスを作るためにモジュール化することができます

Extracting text from webtable selenium webdriver

HashMap<Integer, HashMap<String, String>> tableData = new HashMap<Integer,  HashMap<String, String>>(); 
    HashMap<String, String> tableCellDataMap = null; 
    WebElement table = driver.findElement(By.id("<table_id>")); 

    /** 
    * Call this method to get Table Cell Data by passing WebElement table 
    * @Params table 
    * 
    * @author Fayaz 
    **/ 
    public static HashMap<String, String> getTableData(WebElement table) { 
    List<WebElements> tableColHeaders = table.findElements(By.tagName("th")); 
    List<WebElements> tableRows = table.findElements(By.tagName("tr")); 

    // Start rowIndex with '1' because in the zeroth index we get 'th' tags 
    for(int rowIndex = 1; rowIndex < tableRows.size(); rowIndex++) { 
    List<WebElement> tableCells = tableRows.get(rowIndex).findElements(By.tagName("td")); 
    for(int cellIndex = 0; cellIndex < tableCells.size(); cellIndex++) { 
     String tableCellData = tableCells.get(cellIndex).getText(); 
     String tableColName = tableColHeaders.get(cellIndex).getText(); 
     tableCellDataMap  = new HashMap<String, String>(); 
     tableCellDataMap.put(tableColName, tableCellData); 
    } 
    tableData.put(rowIndex, tableCellDataMap); 
    } 
    return tableCellDataMap; 
} 

)、getRowSize()、 getColSize()、のgetData(int型rowIndexプロパティ)、のgetData(int型colIndex)

我々は複数のテーブルを持っている場合は、ループのテーブルだけでなく

関連する問題