Selenium 从Web表中提取数据
有一个web表,它分布在3个页面上,您可以使用链接遍历到下一个页面 我试图捕获表中的所有数据,但由于站点仅加载第一个页面,因此我只能捕获第一个页面中的数据,其余两个页面被捕获为空白 请建议如何更改此脚本,以便它动态捕获第2页和第3页的数据 我的代码Selenium 从Web表中提取数据,selenium,selenium-webdriver,Selenium,Selenium Webdriver,有一个web表,它分布在3个页面上,您可以使用链接遍历到下一个页面 我试图捕获表中的所有数据,但由于站点仅加载第一个页面,因此我只能捕获第一个页面中的数据,其余两个页面被捕获为空白 请建议如何更改此脚本,以便它动态捕获第2页和第3页的数据 我的代码 driver.get(“https://www.seleniumeasy.com/test/table-pagination-demo.html"); //识别表格 WebElement mytable=driver.findElement(By.x
driver.get(“https://www.seleniumeasy.com/test/table-pagination-demo.html");
//识别表格
WebElement mytable=driver.findElement(By.xpath(“//div[@class='table-responsive']/table/tbody”);
//从表中获取行数
List table_rows=mytable.findElements(按.tagName(“tr”));
int row_count=表_rows.size();
System.out.println(“存在的行数为”+行计数);
//从表中获取列数
List table_cols=driver.findElements(By.xpath(“/*[@id='myTable']]]/tr[1]/td”);
int col_count=表格cols.size();
System.out.println(“存在的列数为”+列数);
//打印表格数据
字符串firstpart=“//*[@id='myTable']/tr[”;
字符串secondpart=“]”/td[”;
字符串thirdpart=“]”;
对于(int row=1;row这是因为第6行等具有display:none;
属性,您需要单击下一步按钮获取文本,添加验证以确保该行是否包含该属性:
for(int row=1;row<=row_count;row++){
//here, the validation
String row_style = table_rows.get(row-1).getAttribute("style");
if(row_style.contains("none")) {
driver.findElement(By.cssSelector(".next_link")).click();
}
for(int col=1;col<=col_count;col++){
String finalXpath = firstpart+row+secondpart+col+thirdpart;
WebElement table_data = driver.findElement(By.xpath(finalXpath));
String table_values = table_data.getText();
System.out.print(table_values+" | ");
}
System.out.println();
}
for(int row=1;row谢谢你的反馈。效果很好!!!你能帮我理解你为什么给出上面的(row-1)吗?@b请你从1
开始循环,所以我只想开始表中的行。从0
获取(索引)
。索引的标准开始是0
。
for(int row=1;row<=row_count;row++){
//here, the validation
String row_style = table_rows.get(row-1).getAttribute("style");
if(row_style.contains("none")) {
driver.findElement(By.cssSelector(".next_link")).click();
}
for(int col=1;col<=col_count;col++){
String finalXpath = firstpart+row+secondpart+col+thirdpart;
WebElement table_data = driver.findElement(By.xpath(finalXpath));
String table_values = table_data.getText();
System.out.print(table_values+" | ");
}
System.out.println();
}