Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
Print multiple texts from multiple elements with same class name

Im trying to print all the product names from an e commerce website in selenium using java but its printing only the first name of the product from the first class it finds. How to print all the product names from all the classes with same class name?

This is my code:

package introduction;

import java.time.Duration;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Getproductnames {

    private static int i;

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

        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "D:/Temp/chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        driver.get("https://www.fipola.in/chicken");
        driver.findElement(By.id("DelLocation")).sendKeys("600020");
        driver.findElement(By.className("top_pincode_select")).click();

        Thread.sleep(3000);

        List<WebElement> products=driver.findElements(By.cssSelector("a.product-item-link"));

        for(int i=0; i<products.size(); i++);
        {
            String[] names = new String[]{products.get(i).getText()};
            System.out.println(names[i] + "");
        }
    }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use the below to print all the products name

List<WebElement> products=driver.findElements(By.cssSelector("a.product-item-link"));


for(int i =0;i<products.size();i++) {
 String elementText = products.get(i).getText(); 
 System.out.println(elementText); 
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!