如何在不替换先前数据的情况下将每个循环实例的新行中的数据写入 Excel 表

我正在尝试将数据从 web 表写入 excel 表并能够将数据写入 excel,但是对于 for 循环的第二个实例,它会覆盖数据或不写入数据,我的要求是将数据写入 excel 到新行中for循环的每个新实例而不是覆盖..有可能..?任何帮助将不胜感激..提前致谢


数据正在写入 excel,但在 reader.setCellData 附近需要帮助


public class DataScraper {


 public static void main(String[] args) throws InterruptedException {



    WebDriver driver = new ChromeDriver();

    driver.manage().window().maximize();

    driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);

    driver.get("https://etrain.info/in?PAGE=LIST--RAJ-TRAINS--1");


    //*[@id="lowerdata"]/table/tbody/tr[1]/td/table/tbody/tr[5]/td[1]/a

    //*[@id="lowerdata"]/table/tbody/tr[1]/td/table/tbody/tr[6]/td[1]/a

    //*[@id="lowerdata"]/table/tbody/tr[1]/td/table/tbody/tr[29]/td[1]/a



    String beforeXpath_TrainNoRow = "//td[@id='content']//tr[";

    String afterXpath_TrainNoRow = "]//td[1]";


    //div[@id='sublowerdata']//tr[3]//td[1]

    //div[@id='sublowerdata']//tr[11]//td[1]


    String beforeXpath_No = "//div[@id='sublowerdata']//tr[";

    String afterXpath_No = "]//td[1]";




    //int rowCount = TrainList.size();


    Xls_Reader reader = new Xls_Reader("C:\\Selenium_Automation\\Projects\\DataDriven_FW\\src\\com\\testdata\\TrainSchedule.xlsx");


    if(!reader.isSheetExist("Rajdhani")) {

        reader.addSheet("Rajdhani");

        reader.addColumn("Rajdhani", "IslNo");

        //reader.addColumn("TrainSearch", "TrainName");



    }


    for(int i = 5; i<= 30; i++) {

        String actualXpath_TrainNoRow = beforeXpath_TrainNoRow + i + afterXpath_TrainNoRow ;

        WebElement TrainNo = driver.findElement(By.xpath(actualXpath_TrainNoRow));

        TrainNo.click();

        Thread.sleep(5000);


        List<WebElement> rows = driver.findElements(By.xpath("//table[@id='schtbl']//tr"));

        System.out.println("total rows is "+ rows.size());


        int rowCount = rows.size();


        for(int j = 3; j<= rowCount - 1; j++) {

 

        }


        driver.navigate().back();

        driver.navigate().refresh();


    }


杨魅力
浏览 88回答 1
1回答

手掌心

嘿不知道你想在这个脚本中实现什么,但这里是更新的脚本。请更新以下代码中的 chrome 驱动程序路径和 excel 文件路径。仅供参考:xpath 可以有效地编写模式,但在这篇文章中没有触及它们。import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.util.List;import java.util.concurrent.TimeUnit;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import org.openqa.selenium.By;import org.openqa.selenium.JavascriptExecutor;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;public class TrainDeatils {&nbsp; &nbsp; public static void main(String[] args) throws InterruptedException, IOException {&nbsp; &nbsp; &nbsp; &nbsp; System.setProperty("webdriver.chrome.driver", "XXXchromedriver path goes here XXX");&nbsp; &nbsp; &nbsp; &nbsp; WebDriver driver = new ChromeDriver();&nbsp; &nbsp; &nbsp; &nbsp; driver.manage().window().maximize();&nbsp; &nbsp; &nbsp; &nbsp; driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);&nbsp; &nbsp; &nbsp; &nbsp; driver.get("https://etrain.info/in?PAGE=LIST--RAJ-TRAINS--1");&nbsp; &nbsp; &nbsp; &nbsp; String beforeXpath_TrainNoRow = "//td[@id='content']//tr[";&nbsp; &nbsp; &nbsp; &nbsp; String afterXpath_TrainNoRow = "]//td[1]";&nbsp; &nbsp; &nbsp; &nbsp; String beforeXpath_No = "//div[@id='sublowerdata']//tr[";&nbsp; &nbsp; &nbsp; &nbsp; String afterXpath_No = "]//td[1]";&nbsp; &nbsp; &nbsp; &nbsp; XSSFWorkbook workbook = new XSSFWorkbook();&nbsp; &nbsp; &nbsp; &nbsp; XSSFSheet spreadsheet = workbook.createSheet( "Rajdhani");&nbsp; &nbsp; &nbsp; &nbsp; XSSFRow header;&nbsp; &nbsp; &nbsp; &nbsp; header = spreadsheet.createRow(0);&nbsp; &nbsp; &nbsp; &nbsp; header.createCell(0).setCellValue("Rajdhani");&nbsp; &nbsp; &nbsp; &nbsp; header.createCell(1).setCellValue("IslNo");&nbsp; &nbsp; &nbsp; &nbsp; int rowNumber = 1;&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 5; i<= 7; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.sleep(1000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String actualXpath_TrainNoRow = beforeXpath_TrainNoRow + i + afterXpath_TrainNoRow ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WebElement TrainNo = driver.findElement(By.xpath(actualXpath_TrainNoRow));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TrainNo.click();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.sleep(5000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<WebElement> rows = driver.findElements(By.xpath("//table[@id='schtbl']//tr"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("total rows is "+ rows.size());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int rowCount = rows.size();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int j = 3; j<= rowCount - 1; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowNumber = rowNumber+1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XSSFRow currentRow = spreadsheet.createRow(rowNumber);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String actualXpath_No = beforeXpath_No + j + afterXpath_No ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String SrNo = driver.findElement(By.xpath(actualXpath_No)).getText();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int islNo = Integer.parseInt(SrNo);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(islNo);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentRow.createCell(0).setCellValue(j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentRow.createCell(1).setCellValue(SrNo);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; driver.navigate().back();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; driver.navigate().refresh();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; FileOutputStream out = new FileOutputStream(new File("XXXexcel file path goes hereXXX\\TrainDetails.xlsx"));&nbsp; &nbsp; &nbsp; &nbsp; workbook.write(out);&nbsp; &nbsp; &nbsp; &nbsp; out.close();&nbsp; &nbsp; &nbsp; &nbsp; driver.close();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java