如何从excel复制字段并使用webdriver / java在网页中搜索它?

时间:2015-12-30 07:12:10

标签: java excel selenium selenium-webdriver

需要有关如何继续以下方案的输入:

  1. 从Excel文件中复制数据(例如:电子邮件地址)
  2. 在网页中搜索
  3. 如果找到复制相应的数据并将其粘贴到另一个excel文件中
  4. 我们可以在带有java的webdriver中执行此操作吗?

1 个答案:

答案 0 :(得分:0)

使用POI罐子 使用getXLcellValue函数获取excel中的值 使用setXLCellValue函数在excel中设置值

参数: -

  • xlpath - > Excel文件的位置
  • sheetName - > Excel文件中的工作表名称
  • rowNum和cellNum - >数据所在的行号和列号 本

         public String getXLcellValue(String xlpath, String sheetName, int rowNum, int cellNum)
            {
                try{
                    FileInputStream fis=new FileInputStream(xlpath);
    
                    Workbook wb=WorkbookFactory.create(fis);
    
                Log.info("Get the value from the cell(getXLcellValue)");
    
                    return wb.getSheet(sheetName).getRow(rowNum).getCell(cellNum).getStringCellValue();
    
            }
            catch(Exception ex)
            {
                Log.info("Error in getXLcellValue ="+ ex.getMessage());
            }
            return "";
        }
    
    
            //set the value of the cell present in specific sheet
    
        void setXLCellValue(String xlpath,String sheetName,int rowNum,int cellNum, String input)
        {
            try{
                FileInputStream fis=new FileInputStream(xlpath);
    
                Workbook wb=WorkbookFactory.create(fis);
    
                wb.getSheet(sheetName).getRow(rowNum).createCell(cellNum).setCellValue(input);
    
                FileOutputStream fos=new FileOutputStream(xlpath);
    
                wb.write(fos);
    
                fos.close();
    
                Log.info("Set the value in cell(setXLCellValue)");
    
            }
            catch(Exception ex)
            {
                Log.info("Error in setXLCellValue ="+ ex.getMessage());
            }
    
        }
    

使用第一个函数从Excel中检索数据。 其次根据您的需要在网络上进行操作 第三个使用set函数在Excel中设置值。

您可以在网上搜索类似的内容

Actions builder = new Actions(driver);
Action select= builder
        .keyDown(Keys.CONTROL)
        .sendKeys("f")
        .keyUp(Keys.CONTROL)
        .sendKeys("ss")
        .build();
select.perform();

OR使用机器人类(不推荐)

driver.get("https://www.google.co.in/");

driver.findElement(By.xpath("//input[@name='btnK']")).sendKeys(Keys.chord(Keys.CONTROL, "f"));
try{
    Robot robot= new Robot();
    robot.keyPress(KeyEvent.VK_W);
    robot.keyRelease(KeyEvent.VK_W);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
}
catch(Exception ex)
{

}

希望它会对你有所帮助:)。