phanbedder使用实例
序
PhantomJS是个好东东,很多时候爬虫需要用到,但是这个有windows、linux、mac的版本,想要跨平台的话,需要自己再处理一下,幸好有了phanbedder帮我们做了这个事情,这样就引一下jar包就完事了。
Selenium以及Phantom JS
Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE、Mozilla Firefox、Chrome等。
Phantom JS是一个服务器端的 JavaScript API 的 WebKit。其支持各种Web标准: DOM 处理, CSS 选择器, JSON, Canvas, 和 SVG。
maven
<dependency> <groupId>net.anthavio</groupId> <artifactId>phanbedder-2.1.1</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>com.github.detro.ghostdriver</groupId> <artifactId>phantomjsdriver</artifactId> <version>1.1.0</version> </dependency>
官网给的这个实际跑起来有点问题,报错
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.browserlaunchers.Proxies at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 29 more
需要更换一下
<dependency> <groupId>net.anthavio</groupId> <artifactId>phanbedder-2.1.1</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.45.0</version> </dependency> <dependency> <groupId>com.codeborne</groupId> <artifactId>phantomjsdriver</artifactId> <version>1.2.1</version> <!-- this will _always_ be behind --> <exclusions> <exclusion> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> </exclusion> <exclusion> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-remote-driver</artifactId> </exclusion> </exclusions> </dependency>
验证
//Phanbedder to the rescue! File phantomjs = Phanbedder.unpack(); DesiredCapabilities dcaps = new DesiredCapabilities(); dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath()); PhantomJSDriver driver = new PhantomJSDriver(dcaps); driver.setLogLevel(Level.INFO); //Usual Selenium stuff... driver.get("https://www.google.com"); WebElement query = driver.findElement(By.name("q")); query.sendKeys("Phanbedder"); query.submit(); System.out.println(driver.getTitle()); driver.quit();
doc
相关推荐
xiangxiaojun 2020-09-23
Reiki 2020-08-16
letheashura 2020-08-14
tiankele0 2020-07-29
curiousL 2020-07-18
王练 2020-07-18
tiankele0 2020-07-18
amei0 2020-07-08
Reiki 2020-07-06
Ronnyxie 2020-07-06
xiangxiaojun 2020-07-05
zhanghaibing00 2020-06-28
xiongyouqiang 2020-06-28
Ronnyxie 2020-06-27
amei0 2020-06-26
letheashura 2020-06-26
tiankele0 2020-06-26
王练 2020-06-25