1 下载PhantomJS
国外的网站下载比较慢,推荐使用淘宝镜像下载,地址:
http://npm.taobao.org/dist/phantomjs/
2 下载PhantomJSDriver
这里使用maven来直接依赖:
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>phantomjsdriver</artifactId>
<version>1.3.0</version>
</dependency>
3 使用Junit来测试下载结果:
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class PhantomJSTest {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
protected static DesiredCapabilities dCaps;
@Before
public void setUp() throws Exception {
// 指定PhantomJS 可执行程序的位置
if("Linux".equals(System.getProperty("os.name"))){
System.setProperty("phantomjs.binary.path", "phantomjs/phantomjs");
}else{
System.setProperty("phantomjs.binary.path", "phantomjs/phantomjs.exe");
}
dCaps = new DesiredCapabilities();
dCaps.setJavascriptEnabled(true);
dCaps.setCapability("takesScreenshot", false);
//<code>dCaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] {"--web-security=no", "--ignore-ssl-errors=yes"});</code>
driver = new PhantomJSDriver(dCaps);
baseUrl = "https://jlkone.1688.com/page/creditdetail.htm";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void getLinksOfAssertSelenium() throws Exception {
driver.get(baseUrl + "/");
//Printing the size, will print the no of links present in the page.
System.out.println("page source: "+driver.getPageSource());
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
}
OK,点击运行Junit,网页就下载下来了。
参考文献:
1. http://www.assertselenium.com/headless-testing/getting-started-with-ghostdriver-phantomjs/