I tried writing 2 List <WebElement> lists to Excel file using Apache POI. But my method only saves the //column 2 values. //column 1 data not saved in the excel file.
Please help me to resolve this issue.
public void FourthExcel(String classNameOne,String classNameTwo) {
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.pollingEvery(2, TimeUnit.SECONDS);
String filePath = System.getProperty("user.dir");
//Book one
XSSFWorkbook workbookOne = new XSSFWorkbook();
XSSFSheet sheetOne = workbookOne.createSheet("Expenses Sheet");
CommonClass.sleepTime(3000);
WebElement expencesTble = driver.findElement(expencesTable);
wait.until(ExpectedConditions.elementToBeClickable(expencesTble));
List<WebElement> ColOneList = driver.findElements(By.className(classNameOne));
List<WebElement> ColTwoList = driver.findElements(By.className(classNameTwo));
List<Object> ColOneobjectList = Arrays.asList(ColOneList.toArray());
List<Object> ColTwoobjectList = Arrays.asList(ColTwoList.toArray());
int ColumnOneSize = ColOneList.size()-1;
for (int i = 0; i <= ColumnOneSize; i++) {
out.println("For");
out.println(ColOneList.get(i).getText());
//Column 1
XSSFRow rowOneColZero = sheetOne.createRow(i);
rowOneColZero.createCell(0).setCellValue(String.valueOf(ColOneobjectList.get(i)));
//Column 2
XSSFRow rowOneCOlOne = sheetOne.createRow(i);
rowOneCOlOne.createCell(1).setCellValue((RichTextString) ColTwoobjectList.get(i));
}
try {
out.println("try");
FileOutputStream fileOut = new FileOutputStream(filePath + "\\testExcel.xls");
workbookOne.write(fileOut);
workbookOne.close();
} catch (IOException e) {
out.println(e);
}
}
The excel file looks like this:
A1 data is not written into the excel file.
In for loop You override XSSFRow row object, please write like this:
for (int i = 0; i <= ColumnOneSize; i++) {
out.println("For");
out.println(ColOneList.get(i).getText());
//create row
XSSFRow row= sheetOne.createRow(i);
//to Column 1
row.createCell(0).setCellValue(String.valueOf( ColOneobjectList.get(i)));
//to Column 2
row.createCell(1).setCellValue((RichTextString) ColTwoobjectList.get(i));
}
Related
Please before you down vote this, I was not able to find an example of reading a web table and writing it to an Excel file. If you happen to find that link kindly provide it. I have found plenty of examples on how to write to an Excel file but without reading from web table part.
Here is my code:
public class WebTable1 {
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.w3schools.com/html/html_tables.asp");
//tr means Row, this table has 7 rows including Header
///td means Column, this table has 3 columns
//*[#id="customers"]/tbody/tr[2]/td[1]
//*[#id="customers"]/tbody/tr[7]/td[1]
//Notice above the pattern, only the values are changing for tr[??]- which is why we will break it down into 2 String
//below and then concatinate them as String
String beforeXpath_Company = "//*[#id='customers']/tbody/tr["; // changed customer to single quote
String aferXpath_Company = "]/td[1]"; //Company is column 1
String beforeXpath_Contact = "//*[#id='customers']/tbody/tr[";
String aferXpath_Contact = "]/td[2]"; // Contact is column 2
String beforeXpath_Country = "//*[#id='customers']/tbody/tr[";
String aferXpath_Country = "]/td[3]"; // Country is column 3
//Find number of rows so that we do not use hard coded values
List<WebElement> totalRows = driver.findElements(By.xpath("//table[#id='customers']//tr"));
int rows=totalRows.size();
for (int i = 2; i <rows; i++) { //we start from 2 because 1 is column name
String actualXpath = beforeXpath_Company + i + aferXpath_Company;
String companyName = driver.findElement(By.xpath(actualXpath)).getText();
System.out.println(companyName);
String actualXpath_Contact = beforeXpath_Contact + i + aferXpath_Contact;
String contactName = driver.findElement(By.xpath(actualXpath_Contact)).getText();
System.out.println(contactName);
String actualXpath_Country = beforeXpath_Country + i + aferXpath_Country;
String countryName = driver.findElement(By.xpath(actualXpath_Country)).getText();
System.out.println(countryName);
//Try to following to write to an Excel file in C drive
Workbook wb = new HSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet1 = wb.createSheet("Sheet1");
Row row = sheet1.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue(createHelper.createRichTextString(companyName));
FileOutputStream fileOut = new FileOutputStream("C:\\MyTemp\\Test.xls");
wb.write(fileOut);
fileOut.close();
}
}
}
Thanks in advance.
Taking your existing table extraction code, you can do something like this:
public class WebTable1 {
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.w3schools.com/html/html_tables.asp");
String beforeXpath_Company = "//*[#id='customers']/tbody/tr["; // changed customer to single quote
String aferXpath_Company = "]/td[1]"; //Company is column 1
String beforeXpath_Contact = "//*[#id='customers']/tbody/tr[";
String aferXpath_Contact = "]/td[2]"; // Contact is column 2
String beforeXpath_Country = "//*[#id='customers']/tbody/tr[";
String aferXpath_Country = "]/td[3]"; // Country is column 3
//Find number of rows so that we do not use hard coded values
List<WebElement> totalRows = driver.findElements(By.xpath("//table[#id='customers']//tr"));
int rows=totalRows.size();
// Create a workbook and a sheet in it
Workbook wb = new HSSFWorkbook();
Sheet sheet1 = wb.createSheet("Sheet1");
// Create a table header
Row row = sheet.createRow(0);
row.createCell(0).setCellValue("Company name");
row.createCell(1).setCellValue("Contact name");
row.createCell(2).setCellValue("Country");
for (int i = 2; i <rows; i++) { //we start from 2 because 1 is column name
String actualXpath = beforeXpath_Company + i + aferXpath_Company;
String companyName = driver.findElement(By.xpath(actualXpath)).getText();
String actualXpath_Contact = beforeXpath_Contact + i + aferXpath_Contact;
String contactName = driver.findElement(By.xpath(actualXpath_Contact)).getText();
String actualXpath_Country = beforeXpath_Country + i + aferXpath_Country;
String countryName = driver.findElement(By.xpath(actualXpath_Country)).getText();
Row row = sheet1.createRow(i - 1);
row.createCell(0).setCellValue(companyName);
row.createCell(1).setCellValue(contactName);
row.createCell(2).setCellValue(countryName);
}
FileOutputStream fileOut = new FileOutputStream("C:\\MyTemp\\Test.xls");
wb.write(fileOut);
fileOut.close();
}
}
I am a beginner in java and Apache POI.
So right now what i wanna to achieve is I want to loop the array days row by row(vertical) under the Days column:
Public Holidays Days Date Class
public static void main(String[] args) {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
String[] days = { "SU", "MO", "TU", "WED", "TH", "FR", "SA" };
Row row = sheet.createRow(0);
row.createCell(0).setCellValue("Public Holidays");
row.createCell(1).setCellValue("Days");
row.createCell(2).setCellValue("Date");
row.createCell(3).setCellValue("Class");
int numRows = sheet.getFirstRowNum();
int numCols = sheet.getRow(0).getLastCellNum();
for (int i = 1; i < 7; i++) {
Row row2 = sheet.createRow(i);
Cell cell = row.createCell(1);
cell.setCellValue(days);
}
try {
FileOutputStream out = new FileOutputStream(new File("C:xx"));
workbook.write(out);
out.close();
System.out.print("Sucess, please check the file");
} catch (Exception e) {
e.printStackTrace();
}
}
The error that I am getting is that:
The method setCellValue(double) in the type Cell is not applicable for the arguments (String[])
Please help me solve this array problem.
The method setCellValue(double) in the type Cell is not applicable for the arguments (String[])
You attempted to pass a String array declared as
String[] days = { "SU", "MO",...};
to the setCellValue() method. There is no overloaded variant of setCellValue() that accepts a String[] argument. I think you meant
cell.setCellValue(days[i-1]);
The error message is a little confusing because in trying to resolve the method it chose one (the one taking double) to indicate in the message.
public static void main(String[] args) {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
String[] days = { "SU","MO", "TU", "WED", "TH", "FR", "SA"};
Row row = sheet.createRow(0);
row.createCell(0).setCellValue("Public Holidays");
row.createCell(1).setCellValue("Days");
row.createCell(2).setCellValue("Date");
row.createCell(3).setCellValue("Class");
int numRows = sheet.getFirstRowNum() + 1;
for (int i = 1; i <= 7; i++) {
Row row2 = sheet.createRow(i);
Cell cell = row2.createCell(1);
cell.setCellValue(days[i - 1]);
}
try {
FileOutputStream out = new FileOutputStream(new File("C:xx"));
workbook.write(out);
out.close();
System.out.print("Sucess, please check the file");
} catch (Exception e) {
e.printStackTrace();
}
}
here is the working file. thank you for helping me :D
I have nearly 1500 rows in web table, I want to write all the rows data to excel.
I'm using Selenium-Webdriver,Java. problem is program stopped without any error after writing nearly 300 rows.
Please help me on this, How to write all the data to excel. if i give thraed.sleep(5000); its taking more time.
I have used below mentioned code:
List<WebElement> irows = a2.findElements(By.xpath("//*[#id='filter_result']/table/tbody/tr/td[2]/a[1]"));
int iRowsCount = irows.size();
System.out.println(iRowsCount);
FileOutputStream fos = new FileOutputStream("E:\\jega\\testw3.xlsx");
XSSFWorkbook wkb = new XSSFWorkbook();
XSSFSheet sheet1 = wkb.createSheet("DataStorage");
String a3=null;
for (int i=500,Row=0;i<=iRowsCount;i++) {
try
{
WebElement val= a2.findElement(By.xpath("//*[#id='filter_result']/table/tbody/tr["+i+"]/td[2]"));
// WebElement val= a2.findElement(By.xpath("//*[#id='filter_result']/table/tbody/tr["+i+"]/td["+j+"]"));
String a = val.getText();
if( a.length() != 0 )
{
a3=a;
//int length = val.length();
System.out.print(a3 + '\n');
XSSFRow excelRow = sheet1.createRow(Row++);
XSSFCell excelCell = excelRow.createCell(0);
excelCell.setCellType(XSSFCell.CELL_TYPE_STRING);
excelCell.setCellValue(a3);
for ( int j=5;j<12;j++)
{ switch (j)
{
case 5:val= a2.findElement(By.xpath("//*[#id='filter_result']/table/tbody/tr["+i+"]/td[5]"));excelCell = excelRow.createCell(1);
excelCell.setCellValue(val.getText()); break;
case 8:val= a2.findElement(By.xpath("//*[#id='filter_result']/table/tbody/tr["+i+"]/td[8]"));excelCell = excelRow.createCell(2);
excelCell.setCellValue(val.getText());break;
case 9:val= a2.findElement(By.xpath("//*[#id='filter_result']/table/tbody/tr["+i+"]/td[9]"));excelCell = excelRow.createCell(3);
excelCell.setCellValue(val.getText());break;
case 11:val= a2.findElement(By.xpath("//*[#id='filter_result']/table/tbody/tr["+i+"]/td[11]"));excelCell = excelRow.createCell(4);
excelCell.setCellValue(val.getText());break;
}
}
Thread.sleep(5000);
}
else
{
System.out.print(a3 + '\n');
XSSFRow excelRow = sheet1.createRow(Row++);
XSSFCell excelCell = excelRow.createCell(0);
excelCell.setCellType(XSSFCell.CELL_TYPE_STRING);
excelCell.setCellValue(a3);
for ( int j=5;j<12;j++)
{ switch (j)
{
case 5:val= a2.findElement(By.xpath("//*[#id='filter_result']/table/tbody/tr["+i+"]/td[5]"));excelCell = excelRow.createCell(1);
excelCell.setCellValue(val.getText()); break;
case 8:val= a2.findElement(By.xpath("//*[#id='filter_result']/table/tbody/tr["+i+"]/td[8]"));excelCell = excelRow.createCell(2);
excelCell.setCellValue(val.getText());break;
case 9:val= a2.findElement(By.xpath("//*[#id='filter_result']/table/tbody/tr["+i+"]/td[9]"));excelCell = excelRow.createCell(3);
excelCell.setCellValue(val.getText());break;
case 11:val= a2.findElement(By.xpath("//*[#id='filter_result']/table/tbody/tr["+i+"]/td[11]"));excelCell = excelRow.createCell(4);
excelCell.setCellValue(val.getText());break;
}
}
Thread.sleep(5000);
}
}//tryc
catch (NoSuchElementException e) {
}//cath
}//for
// System.out.println();
// Thread.sleep(6000);
fos.flush();
wkb.write(fos);
fos.close();
Can anyone help me on this issue?
Your help is much appreciated.
Best Regards.
I have also exported web table data to an excel sheet like this,
This works fine for me. Hope this will help you.
WebElement element = driver.findElement(By.xpath("//*[#id='entireBody']/div[3]/div/table"));
List<WebElement> tbdy = element.findElements(By.tagName("tbody"));
List<WebElement> thed = element.findElements(By.tagName("thead"));
Thread.sleep(1000);
List<WebElement> tr = tbdy.get(0).findElements(By.tagName("tr"));
List<WebElement> trh = thed.get(0).findElements(By.tagName("tr"));
Thread.sleep(1000);
try {
String filename = Path;
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("FirstSheet");
CellStyle style = workbook.createCellStyle();//Create style
Font font = workbook.createFont();//Create font
font.setBoldweight(Font.BOLDWEIGHT_BOLD);//Make font bold
style.setFont(font);//set it to bold
HSSFRow row = sheet.createRow(0);
for(int i = 0; i < trh.size(); i++){
List<WebElement> tdh =trh.get(i).findElements(By.tagName("td"));
System.out.println("loop1");
for (int j = 0; j < tdh.size(); j++) {
row.createCell(j).setCellValue(tdh.get(j).getText());
row.getCell(j).setCellStyle(style);
}
}
for (int i = 0; i < tr.size(); i++) {
HSSFRow row2 = sheet.createRow(i+1);
List<WebElement> td = tr.get(i).findElements(By.tagName("td"));
for (int j = 0; j < td.size(); j++) {
td.get(j).getText();
row2.createCell(j).setCellValue(td.get(j).getText());
}
}
FileOutputStream fileOut = new FileOutputStream(filename);
workbook.write(fileOut);
fileOut.close();
System.out.println("Your excel file has been generated!");
Issue
The below code allows me to either print column headers or print web table data into a csv file depending on if I chose 'th' or 'td' tags but not both simultaneously.
My Question
How do I get it to print to csv both the 'th' and 'td' text at the same time in my CSV output??
Code
I have tried 2 versions of my code, but results are same.Both versions attached.
Code Version 1
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();
}
}
Code Version 2
This version has 2 sets of for loops, first one to print column headers in csv, and second one to print all data from the webtable.
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();
}
}
Change
webRow.findElements(By.tagName("td"))
to
webRow.findElements(By.xpath("//td | //th"))
1) I am trying to read the values from the webtable of an appliation and write it into the excel sheet one by one.
2) There are 4 values in each row of the webtable that need to be written to excel sheet, but there are some images in each row that i'm ignoring bu using below code.
text.length()>2
.
3) There will be 200-300 rows in webtable that need to be fetched and written to excel sheet.
This is the code i have tried. But i dont know how to write it into each row of the excel sheet one by one. Please help me in this regard.
//get the table
WebElement statusTable = browser.findElement(By.id("projectstatus"));
//Get all the rows in the table
List<WebElement> allRows = statusTable.findElements(By.tagName("tr"));
//Get the size(row no) of allRows
int rowSize = allRows.size();
System.out.println(rowSize);
for (WebElement row : allRows) {
//Get all cell values in each row
List<WebElement> allCells = row.findElements(By.tagName("td"));
//System.out.println(allCells.size());
if(allCells.size() > 1){
for (WebElement cell : allCells) {
String text = cell.getText();
if(text.length()>2){
String value = cell.getText();
}
}
}
// locate the test xl file
File file = new File("e:\\Testing_emi.xls");
// create input stream
FileInputStream fis = new FileInputStream(file);
// create workbook
HSSFWorkbook wb = new HSSFWorkbook(fis);
// get sheet
HSSFSheet sheet1 = wb.getSheet("Sheet1");
// get rows
HSSFRow row = sheet1.getRow(1);
HSSFCell cellEx = row.getCell(0);
if (cellEx == null) {
cellEx = row.createCell(0);
}
cellEx.setCellValue(value);
//get the table
WebElement statusTable = browser.findElement(By.id("projectstatus"));
//Get all the rows in the table
List<WebElement> allRows = statusTable.findElements(By.tagName("tr"));
//Get the size(row no) of allRows
int rowSize = allRows.size();
System.out.println(rowSize);
// locate the test xls file
File file = new File("e:\\Testing_emi.xls");
// create input stream
FileInputStream fis = new FileInputStream(file);
// create workbook
HSSFWorkbook wb = new HSSFWorkbook(fis);
// get sheet
HSSFSheet sheet1 = wb.getSheet("Sheet1");
// get rows
HSSFRow row;
for (int i=0; i<rowSize; i++)
{
WebElement webRow = allRows.get(i);
//Get all cell values in each row
List<WebElement> allCells = webRow.findElements(By.tagName("td"));
//System.out.println(allCells.size());
if(allCells.size() > 1)
{
HSSFRow excelRow = sheet1.createRow(i);
for (int j=0; j<allCells.size(); j++)
{
WebElement webCell = allCells.get(j);
String text = webCell.getText();
if(text.length()>2)
{
Cell excelCell = excelRow.createCell();
excelCell.setValue(webCell.getText());
}
}
}
}
sheet1.close();
Read the values from the Web Table of an application and write it in to the excel sheet one by one.There are 5 values in each row of the Web Table and that need to written in Excel sheet. Below is the working code.
System.setProperty("webdriver.chrome.driver","C:\\Users\\Documents\\Selenium jars\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://cosmocode.io/automation-practice-webtable/");
WebElement table = driver.findElement(By.xpath("//*[#id='countries']/tbody"));
List<WebElement> rows = table.findElements(By.tagName("tr"));
int rowcount = rows.size();
FileOutputStream fis = new FileOutputStream(new File("C:\\Users\\Documents\\Selenium\\output.xlsx"));
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sh = wb.createSheet("First Sheet");
for (int row = 0; row < rowcount; row++) {
List<WebElement> columns_inrow = rows.get(row).findElements(By.tagName("td"));
int columns_count = columns_inrow.size();
System.out.println("Number of cells in Row " + row + " are " + columns_count);
Row ro = sh.createRow(row);
for (int column = 0; column < columns_count; column++) {
String celltext = columns_inrow.get(column).getText();
System.out.println(
"Cell Value of row number " + row + " and column number " + column + " Is " + celltext);
ro.createCell(column).setCellValue(celltext);
}
System.out.println("===========================");
}
wb.write(fis);
wb.close();
}