如何使用selenium在Linkedin搜索页面上加载延迟内容

And*_*hai 4 java selenium web-scraping selenium-webdriver

摘要

我想在LinkedIn搜索页面上搜索帐户的所有第一个连接的个人资料链接.但由于页面动态加载其余内容(当您向下滚动时),我无法获得位于页面末尾的"下一页"页面按钮.


问题描述

https://linkedin.com/search/results/people/?facetGeoRegion=["tr%3A0"]&facetNetwork=["F"]&origin=FACETED_SEARCH&page=YOUR_PAGE_NUMBER

我可以使用selenium和上面的链接导航到搜索页面.我想知道有多少页面可以导航它们只需更改page=上面链接的变量.

为了实现我想要检查Next按钮的存在.只要有下一个按钮,我就会请求下一页进行抓取.但是如果你不向下滚动直到页面底部 - 这是'下一步'按钮的位置 - 你找不到Next按钮,也没有找到有关其他配置文件的信息,因为它们尚未加载.

以下是当您不使用firefox截屏工具向下滚动并截取整个页面的截图时的外观.

LinkedIn


我是如何实施的

我可以通过将向下滚动操作硬编码到我的代码中并让驱动程序等待来解决这个问题visibilityOfElementLocated.但我想知道是否还有其他方法比我的做法更好.并且如果通过该方法,驱动程序无法Next以某种方式找到该按钮,则退出代码1退出程序.

当我向下滚动页面时检查请求时,它只是对图像等的请求,如下所示.当我向下滚动页面时,我无法弄清楚页面如何加载有关配置文件的更多信息.

网络检查


源代码

以下是我在代码中实现它的方法.这个应用程序只是一个简单的实现,试图找到Next页面上的按钮.

package com.andreyuhai;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class App 
{
    WebDriver driver;

    public static void main( String[] args )
    {
        Bot bot = new Bot("firefox", false, false, 0, 0, null, null, null);

        int pagination = 1;

        bot.get("https://linkedin.com");
        if(bot.attemptLogin("username", "pw")){
            bot.get("https://www.linkedin.com/" +
                    "search/results/people/?facetGeoRegion=" +
                    "[\"tr%3A0\"]&origin=FACETED_SEARCH&page=" + pagination);


            JavascriptExecutor js = (JavascriptExecutor) bot.driver;

            js.executeScript("scrollBy(0, 2500)");

            WebDriverWait wait = new WebDriverWait(bot.driver, 10);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[@class='next']/div[@class='next-text']")));

            WebElement nextButton = bot.driver.findElement(By.xpath("//button[@class='next']/div[@class='next-text']"));


            if(nextButton != null ) {
                System.out.println("Next Button found");
                nextButton.click();
            }else {
                System.out.println("Next Button not found");
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想知道的另一个工具:LinkedIn Spider

这个chrome扩展名为linkedIn Spider

这也正是我想要实现的,但我想使用JavaScript,我不确定.但是当我在同一个搜索页面上运行此扩展时.这不会向下滚动或逐个加载其他页面提取数据.


所以我的问题是:

  1. 能否请您解释一下LinkedIn如何实现这一目标?我的意思是它如何加载配置文件信息,因为我向下滚动,如果不做任何请求等等.我真的不知道这个.我将不胜感激任何来源链接或解释.

  2. 你有更好的(我的意思更快)想法来实现我想要实现的东西吗?

  3. 能否请你解释一下如何LinkedIn Spider在不向下滚动的情况下工作等等.

Sam*_*ora 6

我检查了div结构和linkedin显示结果的方式.所以,如果你直接点击url并按照xpath检查://li[contains(@class,'search-result')] 你会发现所有结果都已经加载到页面上,但是一次性和滚动只显示了5个结果,它显示了接下来的5个结果但是,所有结果都已加载到页面上,可以通过上述xpath找到.

参考此图像,当您在点击网址时找到输入xpath的结果时,会突出显示div结构和结果:https://imgur.com/Owu4NPh
参考此图像,在滚动页面后突出显示div结构和结果到底部,然后使用相同的xpath查找结果:https://imgur.com/7WNR830

您可以看到结果集相同,但是search-result__occlusion-hint在最后5个结果中<li>标记中还有一个附加部分,并且通过此linkedin隐藏了接下来的5个结果,并且在第一次显示时仅显示前5个结果.

现在来了实现部分,我已经选中了"下一步"按钮,只有当你在页面上滚动整个结果时,所以不是滚动到一个明确的坐标,因为可以为不同的屏幕尺寸和窗口更改,你可以取得结果一个webelement列表并获取它的大小,然后滚动到该列表的最后一个元素.在这种情况下,如果总共有10个结果,则页面将滚动到第10个结果,如果只有4个结果,则页面将滚动到第4个结果,滚动后可以检查下一个按钮是否存在页面与否.为此,您可以检查"下一步"按钮Web元素列表的列表大小,如果列表大小大于0,则表示页面上存在下一个按钮,如果不大于0,则表示下一个按钮列表中没有按钮,您可以在那里停止执行.

所以为了实现它,我采用了一个布尔值,其初始值为true,代码将在循环中运行,直到布尔值变为false,并且当Next按钮列表大小等于0时,它将变为false.

请参考以下代码:

public class App 
{    
    WebDriver driver;

  // For initialising javascript executor
  public Object executeScript(String script, Object... args) {
    JavascriptExecutor exe = (JavascriptExecutor) driver;
    return exe.executeScript(script, args);
  }

  // Method for scrolling to the element
  public void scrollToElement(WebElement element) {
    executeScript("window.scrollTo(arguments[0],arguments[1])", element.getLocation().x, element.getLocation().y);

    }

  public static void main(String[] args) {
    // You can change the driver to bot according to your usecase
    driver = new FirefoxDriver();
    // Add your direct URL here and perform the login after that, if necessary
    driver.get(url);
    // Wait for the URL to load completely
    Thread.sleep(10000);
    // Initialising the boolean
    boolean nextButtonPresent = true;
    while (nextButtonPresent) {
        // Fetching the results on the page by the xpath
        List<WebElement> results = driver.findElements(By.xpath("//li[contains(@class,'search-result')]"));
        // Scrolling to the last element in the list
        scrollToElement(results.get(results.size() - 1));
        Thread.sleep(2000);

        // Checking if next button is present on the page
        List<WebElement> nextButton = driver.findElements(By.xpath("//button[@class='next']"));
        if (nextButton.size() > 0) {
            // If yes then clicking on it
            nextButton.get(0).click();
            Thread.sleep(10000);
        } else {
            // Else setting the boolean as false
            nextButtonPresent = false;
            System.out.println("Next button is not present, so ending the script");
        }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)