【问题标题】:Webscraping a dynamic page which requires scrolling using Selenium in Java网页抓取需要在 Java 中使用 Selenium 滚动的动态页面
【发布时间】:2021-07-23 03:57:36
【问题描述】:

我想查找并打印在此页面上找到的所有背景名称:

https://store.steampowered.com/points/shop/c/backgrounds/cluster/2

我的程序中的问题是我无法滚动到页面底部。我试过了

  1. 操作
  2. 选择一个随机元素对其执行空格键操作
  3. 一堆 Javascript 代码(没有一个在这个网站上工作过)
  4. Robot 方法(我使用该方法发送 pagedown 击键,它确实有效,但我无法在电脑滚动时使用它做任何其他事情)

这是我目前的程序:


public static void steamScraper() {
    
    System.setProperty("webdriver.chrome.driver", "C:\\Users\\user\\Documents\\Selenium\\chromedriver_win32\\chromedriver.exe");
    
    ChromeOptions options = new ChromeOptions();
    options.setBinary("C:\\Users\\user\\Downloads\\chrome-win\\chrome.exe");
    WebDriver driver = new ChromeDriver(options);
    
    driver.get("https://store.steampowered.com/points/shop/c/backgrounds/cluster/2");
    
    driver.manage().window().maximize();
    
    // let the page load for some time
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    
    JavascriptExecutor js = (JavascriptExecutor) driver;
    
    // i = 2200, because there are ~44000 items and 20 items load each scroll, thus 44000 / 20 = 2200
    
    for (int i = 0; i < 10; i++) {
        
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        
        System.out.println(i + 1 + ". " + "scrolling...");
        
    //  here is where I would do the scrolling, and I tried a lot of javascript methods to scroll to the bottom of the page
    //  yet none that I have tried yet would actually perform a scroll (though the method below worked on another site than Steam)
    //
    //  js.executeScript("window.scrollTo(0, document.body.scrollHeight)");

    }
    
    System.out.println("");
    
    // get all div classes containing an item (background)
    
    List<WebElement> nameOfGames = driver.findElements(By.xpath(".//div[@class='rewarditem_AppIconContainer_3Oyyi']//img"));
    
    for (WebElement webElement : nameOfGames) {

        // print the item name (title of img)
        
        System.out.println(webElement.getAttribute("title"));
        
    }
    
    
} ```

【问题讨论】:

  • 以下链接可能会回答您的问题:stackoverflow.com/questions/48850974/… 我通常使用 python 进行网页抓取,但下面的代码被翻译成 java
  • 遗憾的是,没有一个 Java 方法对我有用。
  • 它是崩溃了,还是没有做你想做的事?尝试打印您用于每次锚定滚动的元素的某些属性,看看是否是问题
  • @TomM 我不确定我是否理解正确,但是当我尝试 Zenab Gorach 的方法时,它并没有滚动到页面底部,并且当我在 while 循环中打印一个属性时它只打印一次。这意味着它只会尝试滚动一次。
  • 我最好的猜测是,您可以在页面上找到具有 List nameOfGames = driver.findElements... 行的元素,但是当您实际加载页面时,不会呈现其他元素.因此,您只能移动到页面最初加载时所在的元素列表的末尾。您需要在页面加载下一组后将新元素添加到该列表中,然后滚动到您添加的最低元素。我不熟悉从 selenium 调用 js 脚本,所以我不知道如何提供帮助。

标签: java selenium web-scraping


【解决方案1】:

我想我刚刚想出了解决方案:

我创建了一个新的List&lt;WebElement&gt; gamesAfterFirstScroll = null; 列表。我通过查找我感兴趣的所有元素在 for 循环中使用此列表,然后使用 action.moveToElement(gamesAfterFirstScroll.get(lastGameItem))action.perform() 滚动到最后一项。看起来是这样的:

        List<WebElement> nameOfGames = driver.findElements(By.xpath(".//div[@class='rewarditem_AppIconContainer_3Oyyi']//img"));
        
        Actions action = new Actions(driver);
    
        List<WebElement> gamesAfterFirstScroll = null;
        
        System.out.println("Scrolling.\r\n");
        
        for(int i = 0; i < 50; i++) {

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            
            gamesAfterFirstScroll = driver.findElements(By.xpath(".//div[@class='rewarditem_AppIconContainer_3Oyyi']//img"));
                
            int lastGameItem = gamesAfterFirstScroll.size() - 1;
            
            action.moveToElement(gamesAfterFirstScroll.get(lastGameItem));
            action.perform();
            
            System.out.println(i+1 + ". scroll");
            
        }
            
        for (WebElement e : gamesAfterFirstScroll) {
            
            System.out.println(e.getAttribute("title"));
            nameOfGames.add(e);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-15
    • 2012-01-28
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 2020-06-21
    相关资源
    最近更新 更多