selenium-webdriver - 通过 selenium 使用循环编写 excel 文件
问题描述
我正在尝试将数据从网站写入 excel。该代码正在读取一个输入 Excel 文件并将结果存储在另一个输出文件中。执行我的代码后,我的输出 Excel 文件只包含标题和最后一行数据。由于某种原因,早期的迭代数据没有得到保存。有人可以帮忙吗?
public class clearenceDetailsTest {
public static void main(String[] args) {
// Gecko Driver path
System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
// Initialize Gecko Driver using Desired Capabilities Class
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette",true);
WebDriver driver= new FirefoxDriver(capabilities);
// Launch HRMS
driver.navigate().to("http://hrms.bankofindia.co.in:8050/psp/HRPROD/EMPLOYEE/HRMS/h/?tab=DEFAULT&cmd=login&errorCode=106&languageCd=ENG");
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
// Enter Userid and password
driver.findElement(By.id("userid")).sendKeys("20xxxx");
driver.findElement(By.id("pwd")).sendKeys("0xxxxxx!");
// Click on login
driver.findElement(By.className("psloginbutton")).click();
// Main Menu
driver.findElement(By.id("pthdr2wl")).click();
// Self Service
driver.findElement(By.id("pthnavbca_PORTAL_ROOT_OBJECT")).click();
//View PDC
driver.findElement(By.id("fldra_CO_EMPLOYEE_SELF_SERVICE")).click();
driver.findElement(By.className("pthnavscrolldownshow")).click();
driver.findElement(By.id("crefli_BOI_VIEW_PDC_CMP_GBL")).click();
for (int i=1;i<10;i++) {
driver.switchTo().frame("ptifrmtgtframe");
driver.findElement(By.xpath("//input[starts-with(@Id,'BOI_PEN_EMP_VW_EMPLID')]")).clear();
try {
FileInputStream filein = new FileInputStream(new File("D:\\testdata.xlsx")); // Location of the Excel sheet
XSSFWorkbook workbook = new XSSFWorkbook(filein);
XSSFSheet sheet = workbook.getSheetAt(0); // Sheet 1
String pfNumber = sheet.getRow(i).getCell(0).getStringCellValue();
driver.findElement(By.xpath("//input[starts-with(@Id,'BOI_PEN_EMP_VW_EMPLID')]")).sendKeys(pfNumber);
driver.findElement(By.id("#ICSearch")).click();
String Name=driver.findElement(By.id("PERSONAL_DATA_NAME")).getText();
String Scale=driver.findElement(By.id("BOI_PEN_EMP_VW_GRADE")).getText();
String Zone=driver.findElement(By.id("BOI_ZONE_VW_BOI_NAME")).getText();
String Gender=driver.findElement(By.id("BOI_PEN_EMP_VW_SEX")).getText();
try {
FileOutputStream out =new FileOutputStream(new File("D:\\testdata - Copy.xlsx"));
XSSFWorkbook workbook1 = new XSSFWorkbook();
XSSFSheet sheet1 = workbook1.createSheet("new sheet");
XSSFRow Header = sheet1.createRow(0);
Header.createCell(0).setCellValue(new XSSFRichTextString("PF Number"));
Header.createCell(1).setCellValue(new XSSFRichTextString("Name"));
Header.createCell(2).setCellValue(new XSSFRichTextString("Scale"));
Header.createCell(3).setCellValue(new XSSFRichTextString("Zone"));
Header.createCell(4).setCellValue(new XSSFRichTextString("Gender"));
XSSFRow row1 = sheet1.createRow(i);
row1.createCell(0).setCellValue(new XSSFRichTextString(pfNumber));
row1.createCell(1).setCellValue(new XSSFRichTextString(Name));
row1.createCell(2).setCellValue(new XSSFRichTextString(Scale));
row1.createCell(3).setCellValue(new XSSFRichTextString(Zone));
row1.createCell(4).setCellValue(new XSSFRichTextString(Gender));
workbook1.write(out);
out.close();
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
filein.close();
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
// driver.switchTo().parentFrame();
driver.switchTo().defaultContent();
driver.findElement(By.id("pthnavbccrefanc_BOI_VIEW_PDC_CMP_GBL")).click();
} // for loop
} // main
} // class
解决方案
推荐阅读
- python - 为未来年份生成特定格式的日期时间
- regex - 使用 shell 进行正则表达式组匹配
- python - 我想添加两个 df 列 df['Date'] 和 df['hour'] 来创建列时间戳
- c# - 如何在 C# 中通过 RabbitMQ 队列发送接收文件?
- acumatica - 使用操作\复制订单时重置自定义总和字段的值
- javascript - 期望一个赋值或函数调用,但在调用函数时看到一个表达式
- laravel - 如何从 Laravel 上的数组图像中获取 getClientOriginalName()?
- azure - Microsoft Graph API 与 IMAP/POP3 - 哪个更适合阅读邮件?
- kubernetes - Gitlab CI 无法解析 kubernetes 运行器上的服务主机名
- c# - C# - 如何在两个不同的 dll 中引用相同的类型?