• Selenium Video Tutorials

Selenium WebDriver - 动态网页表格



Selenium WebDriver 可用于处理动态网页表格(行数和列数不相等的表格)。每个表格在 HTML 中都由标签名 table 标识。此外,表格中的每一行都具有标签名为 tr,列的标签名为 td。列标题由 th 标签名标识。

在网页上识别动态网页表格

右键单击在浏览器(例如 Chrome)中打开的网页,然后单击“检查”按钮。这将显示整个页面的完整 HTML 代码。要检查下面页面上的网页表格,请单击下面突出显示的左上方箭头。

Selenium Dynamic Web 1

单击箭头并将其指向表格(如下图所示)后,其 HTML 代码将可用,反映表格标签名以及表格行、列和标题的 tr、td 和 th 标签名。

Selenium Dynamic Web 2

thead 标签下的 th 标签名用于表格中的列标题。

<thead>
   <tr>
      <th scope="col">First Name</th>
      <th scope="col">Last Name</th>
      <th scope="col">Age</th>
      <th scope="col">Email</th>
      <th scope="col">Salary</th>
      <th scope="col">Department</th>
      <th scope="col">Action</th>
   </tr>
</thead>
Selenium Dynamic Web 3

tbody 标签下的 td 标签名用于表格中的单元格值。

Selenium Dynamic Web 4

示例 1

让我们以上面表格为例,我们将使用该表格的定位器 xpath 获取具有 td 标签名的表格的所有单元格值。

语法

Webdriver driver = new ChromeDriver();

// Locate the table element
WebElement table1 = driver.findElement(By.xpath("value of xpath"));

// Find all rows in the table then store in list
List<WebElement> r = table1.findElements(By.xpath(".//tr"));

// Looping through rows and get cell values
for (WebElement rw : r) {
   List<WebElement> cell = rw.findElements(By.xpath(".//td"));
   for (WebElement c : cell) {
      String value = c.getText();
   }
}

代码实现

package org.example;

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

public class HandlingWebTable {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs 
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Open the webpage to identify table
      driver.get("https://tutorialspoint.com/selenium/practice/webtables.php");
      
      // Locate the table element
      WebElement table1 = driver.findElement
         (By.xpath("/html/body/main/div/div/div[2]/form/div[2]/table"));

      // Find all rows in the table
      List<WebElement> r = table1.findElements(By.xpath(".//tr"));

      // Looping through rows and get cell values
      for (WebElement rw : r) {
         List<WebElement> cell = rw.findElements(By.xpath(".//td"));
         for (WebElement c : cell) {
            String value = c.getText();
            System.out.println("Cells values: " + value);
         }
      }

      // Closing browser
      driver.quit();
   }
}

输出

Cells values: Cierra
Cells values: Vega
Cells values: 39
Cells values: cierra@example.com
Cells values: 10000
Cells values: Insurance
Cells values: 
Cells values: Alden
Cells values: Cantrell
Cells values: 45
Cells values: alden@example.com
Cells values: 12000
Cells values: Compliance
Cells values: 
Cells values: Kierra
Cells values: Gentry
Cells values: 29
Cells values: kierra@example.com
Cells values: 2000
Cells values: Legal
Cells values: 
Cells values: Alden
Cells values: Cantrell
Cells values: 45
Cells values: alden@example.com
Cells values: 12000
Cells values: Compliance
Cells values: 
Cells values: Kierra
Cells values: Gentry
Cells values: 29
Cells values: kierra@example.com
Cells values: 2000
Cells values: Legal
Cells values:

Process finished with exit code 0

在上面的示例中,我们捕获了表格中的所有单元格值。

最后,收到消息Process finished with exit code 0,表示代码成功执行。

示例 2

让我们以同一个表格为例,我们将使用该表格的定位器 xpath 获取具有 th 标签名的表格的所有单元格标题。

语法

获取表格中所有单元格标题的语法。

Webdriver driver = new ChromeDriver();
WebElement table1 = driver.findElement(By.xpath("value of xpath"));
List<WebElement> r = table1.findElements(By.xpath(".//tr"));

// Looping through rows and get cell values
for (WebElement rw : r) {
   List<WebElement> cell = rw.findElements(By.xpath(".//th"));
   for (WebElement c : cell) {
      String value = c.getText();
   }
}

代码实现

package org.example;

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

public class HandlingWebTable {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
      
      // Open the webpage
      driver.get("https://tutorialspoint.com/selenium/practice/webtables.php");

      // Locate the table element
      WebElement table1 = driver..findElement
         (By.xpath ("/html/body/main/div/div/div[2]/form/div[2]/table"));

      // Find all rows in the table
      List<WebElement> r = table1.findElements(By.xpath(".//tr"));

      // Looping through rows and get headers
      for (WebElement rw : r) {
         List<WebElement> cell = rw.findElements(By.xpath(".//th"));
         for (WebElement c : cell) {
            String value = c.getText();
            System.out.println("Table headers: " + value);
         }
      }

      // Closing browser
      driver.quit();
   }
}

输出

Table headers: First Name
Table headers: Last Name
Table headers: Age
Table headers: Email
Table headers: Salary
Table headers: Department
Table headers: Action

Process finished with exit code 0

在上面的示例中,我们捕获了表格中的所有表头。

结论

本教程总结了 Selenium WebDriver 动态网页表格的全面介绍。我们从描述如何在 HTML 中识别动态网页表格开始,并通过示例说明如何在 Selenium WebDriver 中处理动态网页表格。这使您能够深入了解 Selenium WebDriver 动态网页表格。最好继续练习您所学的内容,并探索与 Selenium 相关的其他内容,以加深您的理解并拓宽视野。

广告
© . All rights reserved.