Java PhantomJs下载网页

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/

2. https://github.com/detro/ghostdriver

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值