java - 如何使用 Selenium 下载网页源

标签 java selenium

我刚刚开始使用 Selenium Web Driver,但遇到了一个问题:我想将网页的源代码下载到我的 Java 程序中。我尝试过将 driver.getPageSource()HtmlUnit 驱动程序一起使用,但我得到的结果与我手动执行以下操作时得到的结果并不完全匹配:

右键点击浏览器->查看页面源代码。

我无法弄清楚问题出在哪里。是否有不同的 API 适合我的目的,或者我在这里使用了错误的驱动程序?我应该使用 chrome 驱动程序而不是 HtmlUnit 驱动程序吗?如果是,如何使用chrome驱动?

这就是我正在做的事情:

    WebDriver driver = new HtmlUnitDriver();
    driver.get(webPage);
    System.out.println(driver.getPageSource());

最佳答案

我刚刚查看了 Fluent Selenium它使用 Firefox WebDriver。它是一个测试框架,因此不要对断言方法的存在感到惊讶。它可以用于爬行。只需很少的配置就可以完美地工作。它需要 Maven 才能运行,这是我的工作示例:

package fluent;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.seleniumhq.selenium.fluent.FluentWebDriver;
import org.seleniumhq.selenium.fluent.Period;
import org.seleniumhq.selenium.fluent.TestableString;

import java.util.concurrent.TimeUnit;

import static org.openqa.selenium.By.className;

public class Test {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        FluentWebDriver fwd = new FluentWebDriver(driver);

        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.get("http://www.hudku.com/search/business-list/Paint%20%26%20Hardware%20in%20Kanakapura%20Road,%20Bangalore,%20Karnataka,%20India?p=6&h1=mgK%3DFsPlSAsPTaOVwo%2F0FIMA");

        driver.navigate();

        TestableString test = fwd.div(className("heading")).within(Period.secs(3)).getText();

        System.out.println("header: " + test.toString());

        test.shouldContain("Paint");

        System.out.println("all is fine!");
    }
}

我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>testPrj3</groupId>
    <artifactId>testPrj3</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium.fluent</groupId>
            <artifactId>fluent-selenium</artifactId>
            <version>1.14.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>

        <!-- If you're needing Coda Hale's Metrics integration (optional) -->
        <dependency>
            <groupId>com.codahale.metrics</groupId>
            <artifactId>metrics-core</artifactId>
            <version>3.0.0</version>
        </dependency>

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

更新

FluentLenium似乎更受欢迎一点。

关于java - 如何使用 Selenium 下载网页源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19964677/

相关文章:

java - 使用 selenium wait 刷新页面后,当 webelement 的状态从初始值 "xxx"更改为更改值 "yyy"时,如何处理?

selenium - 在 SoapUI 中调用 Selenium?

java - 选择在同一行上运行更新语句时不返回结果?

java - 如何将jsp文件热部署到tomcat?

selenium - 如何使用 Java 在 Selenium Web 驱动程序中输入类型 =Button 时单击按钮

c# - Selenium 2 WebDriver - Chrome - 从通过 JavaScript 设置的文本框中获取值

css - 在 div 下定位一个元素

java - 为什么我的 REST API 在嵌套对象上返回空白 JSON?

java - 玩游戏服务和 LibGDX : How to sign-in correctly?

java - SOLR选择请求错误-Java类转换异常