-4

Need inputs on how to proceed with the below scenario:

  1. Copy data from a excel file(For ex: email address)
  2. Search for it in a web page
  3. if find copy the corresponding data and paste it in another excel file

Can we do this in webdriver with java?

Mukesh Takhtani
  • 852
  • 5
  • 15
user2762008
  • 57
  • 1
  • 3
  • 15
  • 1
    Yes it can be done using selenium webdriver.What you have tried so far? Share code which you have tried to read data from excel and search on web. – Helping Hands Dec 30 '15 at 07:16
  • I just need inputs on how to proceed with this, I have a very rough code ,even if i share that will not make sense – user2762008 Dec 30 '15 at 07:31

1 Answers1

0

Use POI jars Use getXLcellValue function to get the value in excel Use setXLCellValue function to set the value in excel

Parameters :-

  • xlpath -> Location of your Excel file
  • sheetName -> Sheet name in your Excel File
  • rowNum and cellNum -> Row and column number where your data is present

         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());
            }
    
        }
    

Use first function to retrive data from Excel. Secondly do opration on web as per your need Third use set function to set the value in Excel.

You can search on web something like that

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

OR by using robot class(Not recommended )

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)
{

}

Hope it will help you :)

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125