There are some data in webpage in a tabular format.
(网页中有一些表格格式的数据。)
I want to retrieve all and write into an excel. (我想检索所有内容并将其写入Excel。)
I have used a nested for loop. (我使用了嵌套的for循环。)
But the code is only writing only the last column. (但是代码只写了最后一列。)
While trying to retrieve and write onto the console, I could see that all the elements are being written, That means, although the code is taking all elements, i guess it is somehow appending the data and only keeping the latest column. (在尝试检索并写入控制台时,我可以看到所有元素都已写入,这意味着,尽管代码占用了所有元素,但我想它是在某种程度上附加了数据并仅保留最新的列。)
Code used is below. (使用的代码如下。)
Please help if possible. (如果可以的话请帮忙。)
public class FirstTest {
WebDriver driver;
XSSFSheet sheet;
int Rownum;
FileInputStream Fis;
XSSFWorkbook WB;
FileOutputStream fos;
@Test
public void Test1() throws IOException {
Rownum = driver.findElements(By.cssSelector("tr.one")).size();
fos = new FileOutputStream("path to excel");
//System.out.println(Rownum);
for (int i =0; i<Rownum;i++)
{
for (int j=0;j<=3;j++)
{
//System.out.println(driver.findElement(By.xpath("//tr//tr["+(j+1)+"]//td["+(i+1)+"]//div")).getText());
sheet.createRow(i+1).createCell(j).setCellValue(driver.findElement(By.xpath("//tr//tr["+(i+2)+"]//td["+(j+1)+"]//div")).getText());
}
}
WB.write(fos);
fos.close();
}
@BeforeSuite
public void beforeSuite() throws IOException {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
driver.get("https://www.annauniv.edu/dean.php");
Fis = new FileInputStream("path to excel");
WB = new XSSFWorkbook(Fis);
sheet = WB.getSheetAt(0);
}
@AfterSuite
public void afterSuite() throws IOException {
driver.quit();
}
}
ask by Pratilipi Behera translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…