Search code examples
javahtmlmavenselenium-webdriverweb-scraping

Searching for the solution to make selenium scrape products


I'm working on my pet project. As for now, I want to parse product name and its' price. Visit shoppingSite, press button under the map, then press it once again on the right side. Choose upper delivery option and you will be redirected to the shop. Near the search section the menu bar located. Click and choose any category. When you"ll reach the page with products, you can open developers' tool. So, I don't know how to make selenium give me names and prices. What I have tried:

for (int i = 0; i < 6; i++) {
    Thread.sleep(1000);
    JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
    javascriptExecutor.executeScript("window.scrollBy(0, 200)");
    Thread.sleep(1000);
}

webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.category.page__content")));
WebElement element = driver.findElement(By.cssSelector("div.category.page__content"));
System.out.println(element.getText());

Selenium tells me that

Exception in thread "main" org.openqa.selenium.TimeoutException:
Expected condition failed: waiting for visibility of element

Scrolled, waited for the elements' visibility. Did not help me. Will be grateful for any tip.


Solution

  • To get the products names you can do this:

    webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a.product-title")));
    Thread.sleep(300);
    List<WebElement> productNames= driver.findElements(By.cssSelector("a.product-title"));
    List<WebElement> productPrices= driver.findElements(By.cssSelector("div.current-price"));
    

    If you want to get these names and prices as a List of Strings you can use this method:

     public List<String> getElementsListTexts(By element){
        List<String> texts = new ArrayList<>();
        List<WebElement> list = driver.findElements(element);
        for (WebElement el : list ){
            texts.add(el.getText());
        }
        return texts;
    }
    

    So it will be

    webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a.product-title")));
    Thread.sleep(300);
    List<String> names = getElementsListTexts(By.cssSelector("a.product-title"));
    List<String> prices = getElementsListTexts(By.cssSelector("div.current-price"));