I am attempting to extract and print the names of all products from an e-commerce website using Selenium with Java. However, my current code is only fetching the name of the first product it finds within a specific class. How can I modify my code to retrieve the names of all products across multiple classes with the same class name?
Below is the snippet of 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 {
// Setting up Chrome WebDriver
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] + "");
}
}
}