Java 如何使用Selenium下载网页源代码
我刚刚开始使用SeleniumWeb驱动程序,我遇到了一个问题:我想将网页的源代码下载到Java程序中。我已尝试将Java 如何使用Selenium下载网页源代码,java,selenium,Java,Selenium,我刚刚开始使用SeleniumWeb驱动程序,我遇到了一个问题:我想将网页的源代码下载到Java程序中。我已尝试将driver.getPageSource()与HtmlUnit驱动程序一起使用,但得到的结果与我手动执行以下操作时得到的结果不完全匹配: 右键单击浏览器->查看页面源 我想不出是什么问题。是否有一个不同的API用于我的目的,或者我在这里使用了错误的驱动程序?我应该使用chrome驱动程序而不是HtmlUnit驱动程序吗?如果是,如何使用chrome驱动程序 以下是我正在做的:
driver.getPageSource()
与HtmlUnit
驱动程序一起使用,但得到的结果与我手动执行以下操作时得到的结果不完全匹配:
右键单击浏览器->查看页面源
我想不出是什么问题。是否有一个不同的API用于我的目的,或者我在这里使用了错误的驱动程序?我应该使用chrome驱动程序而不是HtmlUnit
驱动程序吗?如果是,如何使用chrome驱动程序
以下是我正在做的:
WebDriver driver = new HtmlUnitDriver();
driver.get(webPage);
System.out.println(driver.getPageSource());
问题是浏览器向Web服务器发送一个字符串,声明它是哪种类型的浏览器,然后网页根据浏览器的不同提供不同的内容。这是基本的web编程事实。开发人员必须根据浏览器调整页面内容,特别是CSS声明。我刚刚检查了使用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!");
}
}
Mypom.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>
4.0.0
testPrj3
似乎更受欢迎。你说“与结果不完全匹配”是什么意思?有什么不同?它是否为您提供了另一页的来源?页面上是否有JavaScript更改内容?我很好奇这是否会影响到这一点。这些是缺少的一些元素。例如“title='SHANKAR HARDWARE in Kanakapura Road,Bangalore'>SHANKAR HARDWARE这并不是我在问题中使用的方法。但当我查看页面源代码时,它会出现..嗨,djangofan,谢谢你出现在我的问题上,预期结果在google chrom上是正确的,所以你是说我必须使用chro吗?”me driver API下载网页的源代码??嗨,Andrey!!!我们已经见过面了,我想,很高兴再次见到你。Maven??我从来没有使用过它。你能给我一些关于这个和你发布的脚本的说明吗..嘿。Maven是最流行的Java构建工具之一,网上有很多关于它的信息。在这个例子中你需要它来从web上获取十几个JAR,并在运行时自动配置你的类路径。我建议你开始在中创建一个新的maven项目。Intellij对我来说非常适合:。这可能看起来是一条很长的路径,但我不知道还有更短的路径……嗨,Andrey,很抱歉我这么晚才回复你。我只是研究一下maven是什么。现在我了解了一点maven。我想问的是,为什么我们要在上面的代码中使用maven???如果我只是在eclipse上运行java而不将其构建到jar中,它会不会不起作用?(我的意思是,maven用于构建jar或类似的东西)嘿,你可以避免使用Maven。我只是希望在这种情况下,需要的罐子列表会让你感到一些痛苦。如果你觉得更容易,就手动下载它们。哦,我明白了……好吧……但我现在已经配置了Maven。很好,我学到了一些新东西……嘿,Andrey,你能帮我一个忙吗???你能点击这个url吗——”“并使用上面提到的程序下载页面源代码??然后寄给我???这样我就能知道这对我的案子是否真的有效??如果你能为我这样做,那将是你方的巨大支持。我知道我要求太多了,但请。。。。摇滚乐。vasanth@gmail.com