如何在 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.com/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