如何在 Selenium 中計算 Web 表格中的標題數?
藉助 findElements() 方法,可以計算 Web 表格中的標題總數。其邏輯是透過表格內的 <<th> 標記,在 xpath 中返回一個 Web 元素列表,然後獲取該列表的大小。
程式碼實現
示例
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import java.util.List;
public class TableHeaderCount {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://tutorialspoint.tw/plsql/plsql_basic_syntax.htm";
driver.get(url);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// xpath with index appended to get the header
//from the row 1 of table with the help of <th> tag
List<WebElement> header = driver.findElements(By.xpath("//table/tbody/tr[1]/th"));
System.out.println(“The number of table headers is “+ header.size());
driver.close();
}
}
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP