Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 98494

Cannot parse several elements using cssSelector Selenium

$
0
0

I'm trying to parse several goods from this link. The problem is I can parse only first element.

To be specific: I need goods which were sorted by "Newest" on the website below. And I click this button before parsing.

   driver.get("https://www.banggood.com/new-arrivals.html");
   driver.findElementByCssSelector("body > div.lastest-box.J-loading-box > div > ul.filtrate.J-filter-list > li:nth-child(2)").click();

So I want to get info about each good doing like:

    // save goods in arrayList
    List<Article> articleList = new ArrayList<>();


    WebElement ulElement = driver.findElement(By.cssSelector("div.box > ul.prt-list.J-prt-list"));
    List<WebElement> liElements = industries.findElements(By.tagName("li"));

    // using loop for parsing
    for (WebElement element : liElements) {
        String price = element.findElement(By.cssSelector("div.price-box > span.price.notranslate")).getText();
        String title = element.findElement(By.cssSelector("p.title")).getText();
        String url = element.findElement(By.cssSelector("span.img > a")).getAttribute("href");
        // switch link to get img 
        String newUrl = url.replace("https://www.banggood.com/new-arrivals.html", url);

        // open new tab using another url
        ((JavascriptExecutor)driver).executeScript("window.open()");
        ArrayList<String> tabs = new ArrayList<>(driver.getWindowHandles());
        driver.switchTo().window(tabs.get(1));
        driver.get(newUrl);
        WebElement img = driver.findElement(By.id("landingImage"));
        String srcImg = img.getAttribute("src");


        articleList.add(new Article(srcImg, url, title, price));

        // close new tab and back to the main window
        ((JavascriptExecutor)driver).executeScript("window.close()");
    }

    driver.close();
    driver.quit();
}

Via debug I see it works:

enter image description here

but loop is going through only first element then the program is freezing. And I don't see any issues in the console.

enter image description here

Can someone tell me what I'm missing here? Thanks in advance.


Viewing all articles
Browse latest Browse all 98494

Latest Images

Trending Articles



Latest Images