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, 2019 in Selenium by sundar
• 120 points
721 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
0 answers
0 votes
0 answers

How to write date text in excel sheet?

May 18, 2020 in Selenium by sukanya
• 120 points
867 views
0 votes
2 answers

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

JavascriptExecutor jsx = (JavascriptExecutor)driver; jsx.executeScript("window.scrollBy(0,555)", ""); or Action classes ...READ MORE

answered Sep 6, 2020 in Selenium by Sri
• 3,190 points
18,925 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,710 points
5,680 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,710 points
18,533 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,710 points
1,680 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,910 points
9,986 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,910 points
9,083 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,460 points
7,748 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP