如何使用Java和Selenium WebDriver處理靜態網頁表格?


為了使用Selenium WebDriver和Java處理靜態網頁表格,必須遵循一系列步驟來提取相關資料並操作表格元件。第一步是使用合適的識別符號找到網頁上的表格。找到後,可以透過HTML標籤(例如<tr>和<td>)訪問各個行和列。

可以透過迭代掃描每一行和每一列來提取網頁表格中的資料並將其儲存起來以便進一步處理。此外,還可以執行諸如單擊特定單元格或驗證表格中是否存在某些資料之類的操作。透過使用Selenium WebDriver和Java,可以更有效地透過自動化來管理靜態網頁表格。

網頁表格

在使用Selenium WebDriver和Java處理網頁表格時,必須與網頁上的HTML表格進行互動。為了適當地找到表格元素,請使用合適的定位器。找到表格後,使用`findElements()`方法檢索其所有行,並透過迴圈迭代它們。再次在此迴圈中使用`findElements()`方法訪問每一行的每一列。然後可以透過`getText()`或`getAttribute()`等方法提取每一列的所需資料。

WebDriver driver = new ChromeDriver();

方法

有多種技術可用於使用Java和Selenium WebDriver處理靜態網頁表格。可以使用以下方法:

  • 使用HTML表格結構

  • 使用XPath軸

使用HTML表格結構

要使用Selenium WebDriver和Java處理靜態網頁表格,可以使用HTML表格結構方法。首先使用其唯一識別符號或任何相關的HTML屬性來標識表格元素。找到後,可以使用WebDriver命令提取表格的行和列,並根據需要進行迭代。透過引用其行和列索引來檢索特定單元格的值。

此外,還可以執行與表格相關的操作,例如按列排序、過濾或搜尋特定資料。透過利用WebDriver的功能和Java程式設計,可以有效地與靜態網頁表格互動,提取資料並無縫地執行各種操作。

演算法

  • 使用WebDriver啟動Web瀏覽器。

  • 導航到包含靜態網頁表格的所需網頁。

  • 使用適當的WebDriver命令(例如,按ID、類、XPath等)找到表格元素。

  • 透過查詢表格中的所有“tr”元素來提取表格行。

  • 使用迴圈迭代行。

  • 在每一行中,根據需要提取表格單元格(“td”元素)或表頭單元格(“th”元素)。

  • 對單元格資料執行所需的操作(例如,檢索文字、驗證值等)。

  • 可選:對錶格執行其他操作,例如排序、過濾或搜尋。

示例

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class TableExample {
   public static void main(String[] args) {
      // Set up WebDriver (Assuming ChromeDriver here)
      System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
      WebDriver driver = new ChromeDriver();

      // Navigate to the desired webpage
      driver.get("https://www.techlistic.com/2017/02/automate-demo-web-table-with-selenium.html");

      // Find the table element
      WebElement tableElement = driver.findElement(By.tagName("table"));
      List<WebElement> rows = tableElement.findElements(By.tagName("tr"));

      // Iterate through each row
      for (WebElement rowElement : rows) {
         List<WebElement> cells = rowElement.findElements(By.tagName("td"));

         // Iterate through each cell in the row
         for (WebElement cellElement : cells) {
            String cellData = cellElement.getText();
            // Process the cell data as needed
            System.out.print(cellData + "\t");
         }

         // Move to the next line after processing each row
         System.out.println();
      }

      // Close the browser
      driver.quit();
   }
}

輸出

Google   Maria Anders        Germany
Meta     Francisco Chang     Mexico
Microsoft    Roland Mendel    Austria
Island Trading    Helen Bennett    UK
Adobe    Yoshi Tannamuri     Canada
Amazon   Giovanni Rovelli     Italy

使用XPath軸

要使用Selenium WebDriver和Java處理靜態網頁表格,可以使用XPath軸,它提供強大的方法來導航和與表格元素互動。透過利用XPath軸,可以找到表格結構內的特定行、列或單元格。“ancestor”、“descendant”和“following-sibling”軸在此上下文中特別有用。

例如,要提取表格行,可以使用XPath表示式“//table//tr”。要檢索行中的特定單元格,可以將行XPath與“td”軸組合,例如“//table//tr[position()=2]//td[position()=3]”。在處理複雜的表格結構時,XPath軸提供了靈活性和精度,使您可以高效地處理靜態網頁表格並準確地提取所需資料。

演算法

  • 使用WebDriver啟動Web瀏覽器。

  • 導航到包含靜態網頁表格的所需網頁。

  • 構建合適的XPath表示式,根據其位置、屬性或內容來定位表格、行、列或單元格。

  • 使用XPath軸(如“ancestor”、“descendant”或“following-sibling”)遍歷表格結構並導航到所需元素。

  • 使用XPath表示式或將軸與位置或屬性條件結合使用,從表格單元格中提取必要的資料。

  • 根據需要處理提取的資料(例如,將其儲存在變數中、執行斷言或輸出它)。

  • 可選:透過相應地調整XPath表示式,對錶格執行其他操作,例如排序、過濾或搜尋。

  • 使用WebDriver命令關閉Web瀏覽器會話。

示例

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class TableExample {
   public static void main(String[] args) {
      // Set up WebDriver (Assuming ChromeDriver here)
      System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
      WebDriver driver = new ChromeDriver();

      // Navigate to the desired webpage
      driver.get("https://www.techlistic.com/2017/02/automate-demo-web-table-with-selenium.html");

      // Retrieve all cells of the table
      List<WebElementa>cells = driver.findElements(By.xpath("//table//tr//td"));

      // Iterate through each cell
      for (WebElement cell : cells) {
         String cellData = cell.getText();
         // Process the cell data as needed
         System.out.print(cellData + "\t");
      }

      // Close the browser
      driver.quit();
   }
}	

輸出

Google   Maria Anders        Germany
Meta     Francisco Chang     Mexico
Microsoft    Roland Mendel    Austria
Island Trading    Helen Bennett    UK
Adobe    Yoshi Tannamuri     Canada
Amazon   Giovanni Rovelli     Italy

結論

在本教程中,我們瞭解到,在使用帶有Java的Selenium WebDriver處理靜態網頁表格時,有多種方法可以有效地處理它們。HTML表格結構方法允許您使用合適的定位器(例如By.tagName())來找到表格元素並遍歷行和單元格。XPath軸方法提供了使用XPath表示式導航HTML結構以查詢所需元素的靈活性。最後,CSS選擇器提供了一種使用CSS選擇器語法來定位和操作表格元素的替代方法。

更新於:2023年7月25日

3K+ 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告