Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

153
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda