简体   繁体   中英

Error printing list of values in excel using selenium and java for a shopping website. Prints only first value

output on console

Prompt on opening excel after execution

output in excel file

I am trying to get top few results from a shopping website(flipkart), I am able to get the results in my compiler but they won't print in the excel. Every time only the first value is printed and the rest is blank. I am trying to print a product's name and it's href link. Again no issue printing in compiler using sysout. With this code only one name of the first element is shown in excel and no links. the link to the website's direct page which I am using is : https://www.flipkart.com/search?sid=tyy%2C4io&otracker=CLP_Filters&p%5B%5D=facets.price_range.from%3DMin&p%5B%5D=facets.price_range.to%3D30000&sort=recency_desc

public static void main(String[] args) throws IOException { 

       // TODO Auto-generated method stub 

        System.setProperty("webdriver.chrome.driver","C:\\Users\\hp\\Desktop\\selenium\\qwertyuiop\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.flipkart.com/search?sid=tyy%2C4io&otracker=CLP_Filters&p%5B%5D=facets.price_range.from%3DMin&p%5B%5D=facets.price_range.to%3D30000&sort=recency_desc");
      
   OutputStream inStream = new FileOutputStream("C:\\Users\\hp\\eclipse-workspace\\firstselenium\\data\\test.xlsx"); 

       XSSFWorkbook wb = new XSSFWorkbook(); 
   
       XSSFSheet sheet = wb.createSheet("new"); 
       XSSFRow row;
       XSSFCell cell;
       
       
       //=sheet.createRow(0);
       List<WebElement> xpaths=driver.findElements(By.xpath("//*[@class='_3wU53n']"));
       List<WebElement> xpathsLink=driver.findElements(By.xpath("//*[@class='_31qSD5']"));
       
       int a=0;
           System.out.println("number of elements: " + xpaths.size()); 
           for(WebElement ele : xpaths){ 
               //ele.sendKeys("value"); 
               
                row = sheet.createRow(0);
               cell = row.createCell(0);
           //  a++;
               cell.setCellValue(ele.getText());
               wb.write(inStream);
               //a++;
               //System.out.println(ele.getText()); 
                
             
           }  
           for(WebElement ele : xpathsLink){ 
               row = sheet.createRow(a);
               cell = row.createCell(1); 
               cell.setCellValue(ele.getAttribute("href"));
               wb.write(inStream);
               a++;
               //System.out.println(ele.getText());
           System.out.println(ele.getAttribute("href"));}

this code has been updated and works fine for printing elements using loop in an excel file. Issue is to print more details in the cells. For instance printing product name, href link, and details in the same row but obviously different cells. To clear the error to print in different rows using loop state the wb.write() command outside the loop.

public static void main(String[] args) throws IOException { 

       // TODO Auto-generated method stub 

        System.setProperty("webdriver.chrome.driver","C:\\Users\\hp\\Desktop\\selenium\\qwertyuiop\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.flipkart.com/search?sid=tyy%2C4io&otracker=CLP_Filters&p%5B%5D=facets.price_range.from%3DMin&p%5B%5D=facets.price_range.to%3D30000&sort=recency_desc");
      
   OutputStream inStream = new FileOutputStream("C:\\Users\\hp\\eclipse-workspace\\firstselenium\\data\\test.xlsx"); 

       XSSFWorkbook wb = new XSSFWorkbook(); 
   
       XSSFSheet sheet = wb.createSheet("new"); 
       XSSFRow row;
       XSSFCell cell;
       
           
List<WebElement> xpaths=driver.findElements(By.xpath("//*[@class='_3wU53n']"));
            List<WebElement> productPoperties=driver.findElements(By.xpath("//div[@class='_3ULzGw']")); 
             List<WebElement> xpathsLink=driver.findElements(By.xpath("//*[@class='_31qSD5']"));    
              int c=0;
              int o=5;
              int m=5;
            int b=0;
            int a=0;
            int n=5;
          System.out.println("number of elements: " + xpaths.size()); 
          
          for(WebElement ele : xpaths){ 
             row = sheet.createRow(a);
            cell = row.createCell(0);
            a++;
            cell.setCellValue(ele.getText());
            System.out.println(ele.getText()); 
            n--;
               if(n==0)break;
          }  
        // wb.write(inStream);
            
          for(WebElement ele : productPoperties){ 
              //ele.sendKeys("value"); 
            
             row = sheet.createRow(b);
            cell = row.createCell(5);
            b++;
            cell.setCellValue(ele.getText());
//          wb.write(inStream);
            //a++;
            System.out.println(ele.getText()); 
            m--;
               if(m==0)break;
            
          }
         // wb.write()
         
          for(WebElement ele : xpathsLink){ 
                row = sheet.createRow(c);
                cell = row.createCell(8); 
                a++;
                cell.setCellValue(ele.getAttribute("href"));
            //  wb.write(inStream);
                c++;
                //System.out.println(ele.getText());
              System.out.println(ele.getAttribute("href"));
              o--;
              if(o==0)break;
              }

          wb.write(inStream);

see whether it works

Please just write wb.write(inStream) after the for loop end. Then text will be fetched from all the elements.

    WebDriver driver=new ChromeDriver();
    driver.get("https://www.flipkart.com/search?sid=tyy%2C4io&otracker=CLP_Filters&p%5B%5D=facets.price_range.from%3DMin&p%5B%5D=facets.price_range.to%3D30000&sort=recency_desc");
    OutputStream inStream = new FileOutputStream("C:\\Users\\hp\\eclipse-workspace\\firstselenium\\data\\test.xlsx"); 
    XSSFWorkbook wb = new XSSFWorkbook(); 
    XSSFSheet sheet = wb.createSheet("new"); 
    XSSFRow row;
    XSSFCell cell;
   
   List<WebElement> xpaths=driver.findElements(By.xpath("//*[@class='_3wU53n']"));
   List<WebElement> xpathsLink=driver.findElements(By.xpath("//*[@class='_31qSD5']"));
   
   int a=0;
   System.out.println("number of elements: " + xpaths.size());            
   for(WebElement ele : xpaths){ 
         
              row = sheet.createRow(a);
              cell = row.createCell(0);
              cell.setCellValue(ele.getText());
              a++;
              System.out.println(ele.getText());               
             
   }  
   for(WebElement ele : xpathsLink){ 
               row = sheet.createRow(a);
               cell = row.createCell(1); 
               cell.setCellValue(ele.getAttribute("href"));
               a++;
               System.out.println(ele.getText());
               System.out.println(ele.getAttribute("href"));
   }
   wb.write(inStream);

           

    


          
    List<WebElement> productName=driver.findElements(By.xpath("//*[@class='_3wU53n']"));
            List<WebElement> productPoperties=driver.findElements(By.xpath("//div[@class='_3ULzGw']")); 
            List<WebElement> productLink=driver.findElements(By.xpath("//*[@class='_31qSD5']"));    
              int c=0;
              int o=5;
              int m=5;
              int b=0;
              int a=0;
              int n=5;
             
          
          for(WebElement ele : productName){ 
             row = sheet.createRow(a);
            cell = row.createCell(0);
            a++;
            cell.setCellValue(ele.getText());
            System.out.println(ele.getText()); 
            n--;
               if(n==0)break;
          }  
          
          for(WebElement ele : productPoperties){   
             row = sheet.getRow(b);
            cell = row.createCell(3);
            b++;
            cell.setCellValue(ele.getText());
            System.out.println(ele.getText()); 
            m--;
               if(m==0)break;
            
          }
          
          for(WebElement ele : productLink){ 
                row = sheet.getRow(c);
                cell = row.createCell(8); 
                cell.setCellValue(ele.getAttribute("href"));
                c++;
              System.out.println(ele.getAttribute("href"));
              o--;
              if(o==0)break;
              }

          wb.write(inStream);
        }

Please see the final code:

    WebDriver driver=new ChromeDriver();
    driver.get("https://www.flipkart.com/search?sid=tyy%2C4io&otracker=CLP_Filters&p%5B%5D=facets.price_range.from%3DMin&p%5B%5D=facets.price_range.to%3D30000&sort=recency_desc");
    OutputStream inStream = new FileOutputStream("/home/arpita/Arpita/flipkartProductsName2.xlsx"); 
    XSSFWorkbook wb = new XSSFWorkbook(); 
    XSSFSheet sheet = wb.createSheet("new"); 
    XSSFRow row;
    XSSFCell cell;
   
    List<WebElement> productName=driver.findElements(By.xpath("//*[@class='_3wU53n']"));
    List<WebElement> productPoperties=driver.findElements(By.xpath("//div[@class='_3ULzGw']")); 
    List<WebElement> productLink=driver.findElements(By.xpath("//*[@class='_31qSD5']"));    
     
    int c=0;
    int b=0;
    int a=0;
    
    System.out.println("number of elements: " + productName.size()); 
     
  
  for(WebElement ele : productName){ 
    row = sheet.createRow(a);
    cell = row.createCell(0);
    a++;
    cell.setCellValue(ele.getText());
    System.out.println(ele.getText()); 
  
  }  
  
  for(WebElement ele : productPoperties){   
    row = sheet.getRow(b);
    cell = row.createCell(1);
    b++;
    cell.setCellValue(ele.getText());
    System.out.println(ele.getText()); 
  
    
  }
  
  for(WebElement ele : productLink){ 
        row = sheet.getRow(c);
        cell = row.createCell(2); 
        cell.setCellValue(ele.getAttribute("href"));
        c++;
      System.out.println(ele.getAttribute("href"));
   
  }

  wb.write(inStream);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM