Java 如何使用webdriver将列标题沿边表数据从web打印到excel
问题 下面的代码允许我打印列标题或将web表数据打印到csv文件中,具体取决于我是选择了“th”还是“td”标记,但不能同时选择两者 我的问题 如何使其在csv输出中同时将“th”和“td”文本打印到csv 代码 我尝试了两个版本的代码,但结果相同。两个版本都附加了 代码版本1Java 如何使用webdriver将列标题沿边表数据从web打印到excel,java,excel,selenium,webdriver,apache-poi,Java,Excel,Selenium,Webdriver,Apache Poi,问题 下面的代码允许我打印列标题或将web表数据打印到csv文件中,具体取决于我是选择了“th”还是“td”标记,但不能同时选择两者 我的问题 如何使其在csv输出中同时将“th”和“td”文本打印到csv 代码 我尝试了两个版本的代码,但结果相同。两个版本都附加了 代码版本1 public class WebToCSV { static WebDriver driver = new FirefoxDriver(); public static void main(String[]
public class WebToCSV {
static WebDriver driver = new FirefoxDriver();
public static void main(String[] args) throws Throwable {
//driver.navigate().to("http://goo.gl/El1PIV");
driver.navigate().to("http://www.bloomberg.com/markets/stocks/futures");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement table = driver.findElement(By.cssSelector
("div[class='data-tables first']"));
List<WebElement> irow = table.findElements
(By.cssSelector("div[class='data-tables first'] tr"));
System.out.println("No. of rows in the table are: " + irow.size());
// Create excel workbook and sheet.
FileOutputStream fos = new FileOutputStream
("/Users/HARSHENDU/Desktop/Selenium_Practice/Stats.csv");
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet ws = wb.createSheet("WriteToXL");
for(int r=0; r<irow.size(); r++) {
WebElement webRow = irow.get(r);
System.out.println(webRow.getText());
XSSFRow row = ws.createRow(r);
List<WebElement> allCells = webRow.findElements(By.tagName("td"));
for(int c=0; c<allCells.size(); c++) {
WebElement webCell = allCells.get(c);
String text = webCell.getText();
XSSFCell excelCell = row.createCell(c);
excelCell.setCellValue(text);
}
System.out.println("");
}
fos.flush();
wb.write(fos);
fos.close();
end();
}
public static void end() {
driver.close();
driver.quit();
}
公共类WebToCSV{
静态WebDriver驱动程序=新的FirefoxDriver();
公共静态void main(字符串[]args)抛出可丢弃的{
//驱动程序。导航()。到(“http://goo.gl/El1PIV");
驱动程序。导航()。到(“http://www.bloomberg.com/markets/stocks/futures");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
WebElement表=driver.findElement(By.cssSelector
(“div[class='data-tables first']”);
列表irow=table.findElements
(通过.cssSelector(“div[class='data-tables first']tr”);
System.out.println(“表中的行数为:”+irow.size());
//创建excel工作簿和工作表。
FileOutputStream fos=新FileOutputStream
(“/Users/HARSHENDU/Desktop/Selenium_Practice/Stats.csv”);
XSSF工作簿wb=新XSSF工作簿();
XSSFSheet ws=wb.createSheet(“WriteToXL”);
对于(int r=0;r变化
到
改变
到
如果你只写一个csv文件,那么使用apache poi来做这件事就太过分了!它只是一个纯文本文件。谢谢你的建议,我最初选择poi是因为我试图在xlsx文件中获得输出,但该文件不会在每次运行脚本时打开。研究表明poi对xlsx的作用类似,而应该使用csv。正如根据您的观察,我将尝试使用缓冲读取器。我想这就是您的意思。非常感谢您的帮助。如果您只编写csv文件,那么使用apache poi来完成这项工作就太过分了!它只是一个纯文本文件。感谢您的建议,我最初选择poi是因为我试图在xlsx文件中获得输出,但是我不会每次运行脚本时都打开该文件。研究表明poi与xlsx类似,而应使用csv。根据您的观察,我将尝试使用缓冲读取器。我想这就是您的意思。非常感谢您的帮助。
public class StatsToxL {
static WebDriver driver = new FirefoxDriver();
public static void main(String[] args) throws Throwable {
//driver.navigate().to("http://goo.gl/El1PIV");
driver.navigate().to("http://www.bloomberg.com/markets/stocks/futures");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement table = driver.findElement(By.cssSelector
("div[class='data-tables first']"));
// Get webtable header column row.
List<WebElement> irow2 = table.findElements
(By.cssSelector("div[class='data-tables first'] thead tr "));
System.out.println("No. of rows in the header are: " + irow2.size());
// Get all webtable rows
List<WebElement> irow = table.findElements
(By.cssSelector("div[class='data-tables first'] tbody tr"));
int iRowCount = irow.size();
System.out.println("No. of rows in the table are: " + iRowCount);
// Create excel workbook and sheet.
FileOutputStream fos = new FileOutputStream
("/Users/HARSHENDU/Desktop/Selenium_Practice/Stats.csv");
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet ws = wb.createSheet("WriteToXL");
// Iterate over webtable header row and header cells.
for(int r2=0; r2<irow2.size(); r2++) {
WebElement webRow2 = irow2.get(r2);
System.out.println(webRow2.getText());
XSSFRow row2 = ws.createRow(r2);
List<WebElement> allCells2 = webRow2.findElements(By.tagName("th"));
for(int c2=0; c2<allCells2.size(); c2++) {
WebElement webCell = allCells2.get(c2);
String text2 = webCell.getText();
XSSFCell excelCell2 = row2.createCell(c2);
excelCell2.setCellValue(text2);
}
System.out.println("");
fos.flush();
wb.write(fos);
}
// Iterate over webtable rows and cells.
for(int r=0; r<iRowCount; r++) {
WebElement webRow = irow.get(r);
System.out.println(webRow.getText());
XSSFRow row = ws.createRow(r);
List<WebElement> allCells = webRow.findElements(By.tagName("td"));
for(int c=0; c<allCells.size(); c++) {
WebElement webCell = allCells.get(c);
String text = webCell.getText();
XSSFCell excelCell = row.createCell(c);
excelCell.setCellValue(text);
}
System.out.println("");
}
fos.flush();
wb.write(fos);
fos.close();
end();
}
public static void end() {
driver.close();
driver.quit();
}
}
webRow.findElements(By.tagName("td"))
webRow.findElements(By.xpath("//td | //th"))