亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在java中使用selenium刮表

在java中使用selenium刮表

一只斗牛犬 2022-06-23 20:27:37
我正在從具有以下格式的事務表中 抓取帳戶的事務:-表格式如果我知道行數,那么我可以遍歷它并通過為每個字段使用單獨的定位器來獲取所需的數據我如何刮掉這些整張桌子,因為我不知道會有多少交易,我需要一些東西,通過它我可以遍歷它并刮掉交易。我正在使用 selenium在 java 中進行抓取。這是交易表的 HTML:-<div id="txn-display"> <!-- Transactions start  --><!--#include virtual="mobile-statement.shtml" -->    <table id="txn-display-table">        <thead>            <tr>                <th>Date</th>                <th colspan="2">Description</th>                <th>Type</th>                <th class="amount-cell">Amount Spent  (<em class="WebRupee">Rs.</em>)</th>                                    </tr>        </thead>        <tbody>            <tr class="gridEven">                <td>12/02/2019</td>                <td colspan="2" class="word-break">INTERGLOBE AVIATION LT .             IND</td>                <td class="txn-type">Debit</td>                <td class="amount-cell">320</td>                                    </tr>            <tr class="gridOdd">                <td>27/01/2019</td>                <td colspan="2" class="word-break">PETROL TRXN FEE RVRSL EXCLUDING TAX</td>                <td class="txn-type">Credit</td>                <td class="amount-cell">8.21</td>                                   </tr>            <tr class="gridEven">                <td>27/01/2019</td>                <td colspan="2" class="word-break">SHELL R K R ENTERPRISE BANGALORE     IND</td>                <td class="txn-type">Debit</td>                <td class="amount-cell">831.06</td>                                 </tr>        </tbody>    </table>        </div>
查看完整描述

2 回答

?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

下面提到的代碼將自動計算表中提到的行和列。它適用于帶有tr和td標記名的表。您只需要將 web 表 xpath 傳遞給代碼。


@Test 

public void testWebTable()  { 

WebElement simpleTable = driver.findElement(By.xpath("//table[@id='txn-display-table']//tbody")); 


    // Get all rows 

    List<WebElement> rows = simpleTable.findElements(By.tagName("tr")); 

    Assert.assertEquals(rows.size(),4); 


    // Print data from each row 

    for (WebElement row : rows) { 

        List<WebElement> cols = row.findElements(By.tagName("td")); 

        for (WebElement col : cols) {

             System.out.print(col.getText() + "\t"); 

           } System.out.println(); 

       }

    }


查看完整回答
反對 回復 2022-06-23
?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

您尚未發布 HTML,因此以我自己的示例為例,我正在使用 row 和 col 計數進行迭代,請檢查并讓我們知道您是否有任何疑問..


package Testng_Pack;


import java.util.concurrent.TimeUnit;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;


public class table { 


 WebDriver driver = null;

 @BeforeTest

    public void setup() throws Exception { 

         System.setProperty("webdriver.gecko.driver", "D:\\Selenium Files\\geckodriver.exe");

  driver = new FirefoxDriver();

         driver.manage().window().maximize();

         driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

         driver.get("Pass the URL here"); 

    } 


  @AfterTest

 public void tearDown() throws Exception { 

   driver.quit();

     } 


 @Test

 public void print_data(){


 //Get number of rows In table.

 int Row_count = driver.findElements(By.xpath("//*[@id='post-body-6522850981930750493']/div[1]/table/tbody/tr")).size();

 System.out.println("Number Of Rows = "+Row_count);


 //Get number of columns In table.

 int Col_count = driver.findElements(By.xpath("//*[@id='post-body-6522850981930750493']/div[1]/table/tbody/tr[1]/td")).size();

 System.out.println("Number Of Columns = "+Col_count);


 //divided xpath In three parts to pass Row_count and Col_count values.

 String first_part = "//*[@id='post-body-6522850981930750493']/div[1]/table/tbody/tr[";

 String second_part = "]/td[";

 String third_part = "]";


 //Used for loop for number of rows.

 for (int i=1; i<=Row_count; i++){

  //Used for loop for number of columns.

  for(int j=1; j<=Col_count; j++){

   //Prepared final xpath of specific cell as per values of i and j.

   String final_xpath = first_part+i+second_part+j+third_part;

   //Will retrieve value from located cell and print It.

   String Table_data = driver.findElement(By.xpath(final_xpath)).getText();

   System.out.print(Table_data +"  ");   

  }

   System.out.println("");

   System.out.println("");  

 } 

 }

}


查看完整回答
反對 回復 2022-06-23
  • 2 回答
  • 0 關注
  • 180 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號