java - 如何使用selenium优化java中的向下滚动代码

标签 java javascript selenium web-crawler jsoup

我正在使用JavaMAVEN中工作。 我必须获取一个 URL,向下滚动它们,并获取此给定网页中其他项目的所有链接。

到目前为止,我使用 Selenium 动态获取页面,向下滚动它们,并获取链接。但这需要太多时间。请帮我优化一下。

示例:-,我正在处理一个页面,其链接为 here .

我的问题:-

  1. 使用 selenium 滚动网页非常慢。我该如何优化这个? (建议任何其他方法
    做同样的事情或帮助我优化这个)

提前致谢。期待您的友好回复。

动态获取和滚动页面的代码:-

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import com.google.common.collect.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

/**
 *
 * @author jhamb
 */
public class Scroll_down {

    private static FirefoxProfile createFirefoxProfile() {
        File profileDir = new File("/tmp/firefox-profile-dir");
        if (profileDir.exists()) {
            return new FirefoxProfile(profileDir);
        }
        FirefoxProfile firefoxProfile = new FirefoxProfile();
        File dir = firefoxProfile.layoutOnDisk();
        try {
            profileDir.mkdirs();
            FileUtils.copyDirectory(dir, profileDir);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return firefoxProfile;
    }



    public static void main(String[] args)  throws InterruptedException{
        String url1 = "http://www.jabong.com/men/shoes/men-sports-shoes/?source=home-leftnav";
        System.out.println("Fetching %s..." + url1);
        WebDriver driver = new FirefoxDriver(createFirefoxProfile());


        driver.get(url1);  

        JavascriptExecutor jse = (JavascriptExecutor)driver;
        jse.executeScript("window.scrollBy(0,250)", "");
        for (int second = 0;; second++) {
            if (second >= 60) {
                break;
            }
            jse.executeScript("window.scrollBy(0,200)", "");
            Thread.sleep(1000);
        }
            String hml = driver.getPageSource();
        driver.close();


        Document document = Jsoup.parse(hml);

            Elements links = document.select("div");

        for (Element link : links) {
            System.out.println(link.attr("data-url"));
        }
    }
}

最佳答案

Selenium 滚动是基于 Javascript 的。我不知道你的 Selenium 目标,但你没有断言来比较代码中的任何内容? 当您确信数据获取速度如此之快时,请不要使用任何 sleep 方法。 sleep 方法使 Selenium 变慢,但是它正在等待元素正确加载...... 测试什么取决于你

关于java - 如何使用selenium优化java中的向下滚动代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15919602/

相关文章:

java - JMeter 使用 JavaImported 导入定义的 JAR

javascript - 如何在 Three.js 中将场景或模型导出为 GLTF 格式?

java - 带有 JPA 存储库的 Spring Boot 静态 where 子句

java - 如何在 Java BlackBerry 中将编码的 jpeg 图像保存到文件

javascript - 我是否需要使用 documentFragment 来插入 UL 列表

python - 如何使用带有 phantomjs 的 Selenium 从新窗口获取 url

Java Selenium Web 元素变量与 Web 元素数组

python - 使用 python 在 Selenium 中可见后单击按钮

java - 使用后退按钮离开 GuidedStepFragment 时出现空白屏幕

javascript - 将作为字符串从 API 返回的正则表达式转换为 JavaScript 中的有效 RegEx 对象