Reputation: 3
I've got a list of webelements and I need to write every element form the list in separate cell in Excel. Currently when for example i have 2 elements in a list, second one deletes first element in cell.
List<WebElement> products = driver.findElements(By.xpath("//label[contains(text(),'Size')]"));
for (WebElement product:products){
SetCellData(product.getText(),1,0);
Upvotes: 0
Views: 421
Reputation: 1132
so it depends on how you want the data.
i'm guessing you are using apache poi. or something similar
your loop doesn't move around the workbook it just writes to the same cell
here is a example i pulled from a tutorial
//This data needs to be written (Object[])
Map< String, Object[] > empinfo =
new TreeMap<>();
empinfo.put( "1", new Object[] { "EMP ID", "EMP NAME", "DESIGNATION" });
empinfo.put( "2", new Object[] { "tp01", "Gopal", "Technical Manager" });
empinfo.put( "3", new Object[] { "tp02", "Manisha", "Proof Reader" });
empinfo.put( "4", new Object[] { "tp03", "Masthan", "Technical Writer" });
empinfo.put( "5", new Object[] { "tp04", "Satish", "Technical Writer" });
empinfo.put( "6", new Object[] { "tp05", "Krishna", "6546984651685465132198651324984351" });
//Iterate over data and write to sheet
Set< String > keyid = empinfo.keySet();
int rowid = 0;
for (String key : keyid) {
row = spreadsheet.createRow(rowid++);
Object [] objectArr = empinfo.get(key);
int cellid = 0;
for (Object obj : objectArr) {
Cell cell = row.createCell(cellid++);
cell.setCellValue((String)obj);
}
}
so if you want each element in 1 row. you will do Cell cellToFill = row.createCell(nextCellColNumber)
if you want a vertical list it'll be create Row create cell(0) and set value there
Upvotes: 1