How to write the results in excel

0 votes

Hi,

Can any one help me to create a selenium script for the below testing requirement.

1. Open chrome and navigate to google .com

2. Read the search strings(selenium,UFT,Loadrunner) from excel spread sheet and perform search. 

3.Capture the serach results for each string and write into the same excel against the search string( for example- Selenium(column1) and  serach results(About 50,400,000 results (0.61 seconds) ) in next column

I created the below code but it is working only for 1 row of data:

Code:

System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");

               driver = new ChromeDriver();

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

             WebElement element = driver.findElement(By.name("q"));

       

        FileInputStream file = null;

        try {

            file = new FileInputStream(ExcelPath);

             }

         catch (FileNotFoundException e)

        {

                        e.printStackTrace();

        }

        try {

                        book = WorkbookFactory.create(file);

        } catch (IOException e) {

                        e.printStackTrace();

        }

        sheet = book.getSheet(sheetname);

        Object data[][] = new Object[sheet.getLastRowNum()][sheet.getRow(0).getLastCellNum()];

      

        for(int i=0; i<sheet.getLastRowNum(); i++){

                        for(int k=0; k<sheet.getRow(0).getLastCellNum(); k++){

                          

                                        data[i][k] = sheet.getRow(i).getCell(k).toString();

                                        element.sendKeys(data[i][k].toString());

                                        element.submit();

                                       

                                        String searchResults = driver.findElement(By.xpath("//div[@id='resultStats']")).getText();

                                        Thread.sleep(4000);

                                     

                                     

                                        int col=k+1;

                                      

                                        ExcelUtil.writeexcel(ExcelPath,sheetname,searchResults, i,col);

                                        Thread.sleep(2000);

                                        driver.navigate().back();

                                      

                                        Thread.sleep(3000);                  

                                        element.sendKeys(data[i+1][k].toString());

                                        element.submit();

                                      

                        }

public static void writeexcel(String ExcelPath, String SheetName,String text , int rowno , int colno) {

         try {

                File f= new File(ExcelPath);

                if(!f.exists()){

                    System.out.println("File not Found so new file created");

                    Workbook wb = new XSSFWorkbook();

                    FileOutputStream fileOut = new FileOutputStream(ExcelPath);

                    wb.createSheet(SheetName);

                    wb.write(fileOut);

                    wb.close();

                    fileOut.close();                 

                }

                System.out.println("File Found so updating the existing file");

                 fis = new FileInputStream(ExcelPath);

                 wb = WorkbookFactory.create(fis);

                // sh = wb.createSheet(SheetName);

                sh = wb.getSheet(SheetName);

                 if(sh == null){

                     sh = wb.getSheet(SheetName);

                 }

                 }catch(Exception e)

                 {System.out.println(e.getMessage());

                 }

         

         try{

             

               row = sh.getRow(rowno);

               if(row == null){

                   row = sh.createRow(rowno);

               }

               cell = row.getCell(colno);

               if(cell!=null){

                   cell.setCellValue(text);

               }

               else{

                   cell = row.createCell(colno);

                   cell.setCellValue(text);

               }

               fos = new FileOutputStream(ExcelPath);

               wb.write(fos);

               fos.flush();

               fos.close();

           }catch(Exception e){

               System.out.println(e.getMessage());

           }

         

      }

Thanks

Sun

Nov 20 in Selenium by sundar
• 120 points
84 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Selenium

0 votes
1 answer

How to scroll the Page up or down in Selenium WebDriver? (using java)

Its pretty simple. Check out the below ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,110 points
8,826 views
0 votes
1 answer

How to open a browser windows silently in the background?

It is a little bit of a ...READ MORE

answered Apr 20, 2018 in Selenium by king_kenny
• 3,650 points
396 views
0 votes
1 answer

How to handle the popup of file upload in Windows OS with WebDriver?

The value of name locator has probably ...READ MORE

answered Apr 25, 2018 in Selenium by king_kenny
• 3,650 points
4,147 views
0 votes
1 answer

How to choose the correct iframe in Selenium WebDriver?

When I checked the website you've mentioned, ...READ MORE

answered Apr 29, 2018 in Selenium by king_kenny
• 3,650 points
314 views
0 votes
1 answer

How to eliminate this error”Cannot instantiate the type Select in selenium webdriver”

Try below code. Select sc = new Select(driver.findElement(By.xpath("your ...READ MORE

answered May 18, 2018 in Selenium by Samarpit
• 5,150 points
1,931 views
0 votes
1 answer

How to scroll until the element is in view using Selenium2Library Keyword?

You have to download the ExtendedSelenium2Library from ...READ MORE

answered Jun 25, 2018 in Selenium by Samarpit
• 5,150 points
2,754 views
0 votes
1 answer

How to wait till the text is present In text field

Use Waits in Selenium WebDriver to wait ...READ MORE

answered Jul 5, 2018 in Selenium by Meci Matt
• 9,430 points
525 views
0 votes
1 answer

How to wait for the webpage to load in selenium using java

Firstly, when you launch a web application, ...READ MORE

answered Aug 28, 2018 in Selenium by bug_seeker
• 15,360 points
245 views
0 votes
1 answer