htmlunitwebdriver htmlunit为什么官方移除了

1279人阅读
爬虫(14)
& & & & Proxy proxy = new Proxy();
& & & & //设置代理服务器地址
& & & & proxy.setHttpProxy(&代理服务器地址:端口号&);
& & & & DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
& & & & capabilities.setCapability(CapabilityType.PROXY, proxy);
& & & & WebDriver driver = new HtmlUnitDriver(capabilities)
& & & & & & @Override
& & & & & & protected WebClient modifyWebClient(WebClient client)
& & & & & & {
& & & & & & & & DefaultCredentialsProvider creds = new DefaultCredentialsProvider();
& & & & & & & & creds.addCredentials(&用户名&, &密码&);
& & & & & & & & client.setCredentialsProvider(creds);
& & & & & & & &
& & & & & & }
& & & & };
& & & & driver.get(&&);
& & & & System.out.println(&[& + driver.getTitle() + &]&);
&&相关文章推荐
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:608581次
积分:9245
积分:9245
排名:第1756名
原创:269篇
转载:221篇
评论:54条
(6)(4)(3)(10)(9)(2)(3)(2)(1)(4)(21)(3)(10)(1)(3)(1)(10)(15)(29)(34)(16)(3)(7)(4)(8)(7)(2)(6)(24)(10)(2)(5)(9)(3)(1)(194)(1)(4)(3)(2)(7)htmlunit和爬虫问题 - 小笔记 - ITeye技术网站
博客分类:
最近写了个爬虫项目,暂时放在github上。
对于爬虫最难的问题应该是javascript和ajax的处理。现在很多网站使用大量ajax,普通爬虫无法获取js生成的
目前大体上有2中方式来解决这个问题。
一是使用htmlunit之类的所谓handless browser。htmlunit对js支持相当不错。只是适用起来不很方便。想要知道什么时候js执行完了比较困难。
官网 FQA对这个问题的说明。
Nothing happens when using HtmlUnit with AJAX, although page works correctly in browsers. What's wrong?
The main thread using HtmlUnit may be finishing execution before allowing background threads to run. You have a couple of options:
webClient.setAjaxController(new NicelyResynchronizingAjaxController()); will tell your WebClient instance to re-synchronize asynchronous XHR.
webClient.waitForBackgroundJavaScript(10000); or webClient.waitForBackgroundJavaScriptStartingBefore(10000); just after getting the page and before manipulating it.
Explicitly wait for a condition that is expected be fulfilled when your JavaScript runs, e.g.
//try 20 times to wait .5 second each for filling the page.
for (int i = 0; i & 20; i++) {
if (condition_to_happen_after_js_execution) {
synchronized (page) {
page.wait(500);
我的测试:
public static void testYouku() throws Exception {
String url = "/v_show/id_XNDc2MDkzMTIw.html";
String xurl = "/v_vpofficiallistv5/id__showid_271942_page_2?__rt=1&__ro=listitem_page2";
// String a = "&a page=\"2\"&178-101&/a&";
// String url="";
// 模拟一个浏览器
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17);
LogFactory.getFactory().setAttribute("mons.logging.Log","mons.logging.impl.NoOpLog");
java.util.logging.Logger.getLogger("net.sourceforge.htmlunit").setLevel(java.util.logging.Level.OFF);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
// final WebClient webClient=new
// WebClient(BrowserVersion.FIREFOX_10,"http://myproxyserver",8000);
// //使用代理
// final WebClient webClient2=new
// WebClient(BrowserVersion.INTERNET_EXPLORER_10);
// 设置webClient的相关参数
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setActiveXNative(false);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.waitForBackgroundJavaScript(600*1000);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getOptions().setJavaScriptEnabled(true);
webClient.setJavaScriptTimeout();
webClient.getOptions().setRedirectEnabled(true);
webClient.getOptions().setThrowExceptionOnScriptError(true);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(true);
webClient.getOptions().setTimeout();
webClient.waitForBackgroundJavaScript(600*1000);
webClient.waitForBackgroundJavaScript(600*1000);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
// 模拟浏览器打开一个目标网址
final HtmlPage page = webClient.getPage(url);
该方法在getPage()方法之后调用才能生效
webClient.waitForBackgroundJavaScript(1000*3);
webClient.setJavaScriptTimeout(0);
Thread.sleep(1000 *3L);
String js = "javascript:checkShowFollow('271942','2');";
ScriptResult sr = page.executeJavaScript(js);
HtmlPage newPage = (HtmlPage) sr.getNewPage();
System.out.println("new page.asText=" + newPage.asText());
System.out.println("page.asText=" + page.asText());
System.out.println("page.getUrl=" + page.getUrl());
List links = (List) page.getByXPath ("//*[@id=\"groups_tab\"]/div[1]/ul/li[1]/a");
if(null!=links){
System.out.println(links.size());
HtmlAnchor link = (HtmlAnchor) links.get(0);
System.out.println(link.asXml());
HtmlPage p = link.click();
webClient.waitForBackgroundJavaScript(1000*3L);
webClient.waitForBackgroundJavaScriptStartingBefore(1000L);
Thread.sleep(3000L);
System.out.println(p.asText());
youku上面超过100集会拆分多个列表展示,全部是js生成的。
上面代码执行的成功率不是很高。
19:25:58 Re-synchronized call to /v_vpofficiallistv5/id__showid_271942_page_2?__rt=1&__ro=listitem_page2
19:26:00 Expected content type of 'application/javascript' or 'application/ecmascript' for remotely loaded JavaScript element at 'http://html./html?p=74,75,558,747,753,850,,101564&ct=h&cs=09&td=0&s=271942&v=&u=&k=%E7%BD%91%E7%90%83%E7%8E%8B%E5%AD%90|&sid=8i0r&tt=网球王子 001&pu=/v_show/id_XNDc2MDkzMTIw.html&ref=', but got 'text/html'.
其实只要能拿到Re-synchronized call to后面的URL后直接使用httpclient就可以了。但是htmlunit没有提供相关方法。debug了一下源码还是没找到什么时候这个URL被生成的(这个URL肯定是js函数中生成并异步调用的)。
继续研究。。。
第二种方法是使用selenium,jdic直接调用浏览器。
这两种方式都太慢。相对来说第一种方式效率比第二种要好一些。
下载次数: 169
下载次数: 27
浏览 28652
浏览: 160982 次
来自: 北京
能整个好点的
对js的支持没你说的那么好,你用他取爬下淘宝
请问下ebean可以执行原生sql语句吗?
楼主威武,正好解决了我的问题。
string2020 写道
public class Appl ...#抓取内容WebDriver driver = new HtmlUnitDriver(false);
driver.get(url);
String html = driver.getPageSource();#如何想等待一会元素渲染完毕
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
#进行百度搜索
public static void doSearch(String keyword) {
final String url = "";
WebDriver driver = new HtmlUnitDriver(false);
driver.get(url);
driver.findElement(By.id("kw")).sendKeys(keyword);
Actions action = new Actions(driver);
action.sendKeys(Keys.ENTER).perform();
System.out.println(driver.getPageSource());
1 Selenium可支持的【真实】浏览器驱动:
  PC端驱动:firefox、safari、、、&driver
  移动端驱动:&、、、& 支持iphone、ipad、android、FirefoxOS【第三方】
  safari、ff都是以插件形式驱动浏览器本身的;ie、chrome都是通过二进制文件来驱动浏览器本身的;
  这些driver都是直接启动并通过调用浏览器的底层接口来驱动浏览器的,因此具有最真实的用户场景模拟,主要用于进行web的兼容性使用。
2 selenium可支持的【伪浏览器】驱动:
  HtmlUnit&、&
  它们都不是真正的在浏览器、都没有GUI,而是具有支持html、js等解析能力的类浏览器程序;这些程序不会渲染出网页的显示内容,但是支持页面元素的查找、JS的执行等;由于不进行css及GUI渲染,所以运行效率上会比真实浏览器要快很多,主要用在功能性测试上面。htmlunit是实现的类浏览器程序,包含在selenium server中,无需驱动,直接实例化即可;其js的解析引擎是Rhino。
阅读(...) 评论()selenium使用中遇到的问题:关于FirefoxDriver和HtmlUnitDriver共享登陆状态
来源:互联网
编辑:刘梓楠
有网友碰到过这样的问题:selenium使用中遇到的问题:关于FirefoxDriver和HtmlUnitDriver共享登陆状态,问题详细内容为:WebDriver fireFoxDriver = new FirefoxDriver() ;WebDriver htmlUnitDriver = new HtmlUnitDriver() ;
最近在使用selenium对网站进行测试,使用到FirefoxDriver和htmlUnitDriver,现在遇到一个问题想和大家探讨一下:
1.各新建一个对象,如上述代码。
2.使用firefoxDriver这个对象对网站进行登陆。
问题:有没办法使htmlUnitDriver也有具体了网站登陆后的状态,不需要重新登陆就可以直接访问后台。
我尝试将firefoxDriver对像的Cookies赋值给htmlUnitDriver,但是不成功。代码如下:
Set&Cookie& allCookies = fireFoxDriver.manage().getCookies();for(Cookie c:allCookies){&&&&Cookie cookie = new Cookie(c.getName(),c.getValue()) ;&&&&htmlUnitDriver.manage().addCookie(cookie) ;}
各位大神有时间给我解答下,不胜感激。
,我搜你通过互联网收集了相关的一些解决方案,希望对有过相同或者相似问题的网友提供帮助,具体如下:解决方案1:题主,问题解决了吗?遇到同样问题求教啊
请注意,本站信息均收集自互联网,相关信息仅供参考,医疗等重要信息请以正规途径为最终意见,本站不承担任何责任!
您可能还关注工具(25)
& & & &In my paper,I need &to distinguish the difference between&Selenium-WebDriver and HtmlUnit.So
google that!&The answer comes from stackoverflow:
& & & &HtmlUnit is a java based implementation of a WebBrowser without a GUI and a way to simulate a browser for testing purposes and Selenium-WebDriver makes direct calls to the browser using each browser’s
native support for automation. we can see that HtmlUnit provides API without GUI possibility for automation whereas WebDriver provides internal browsers' possibilities for automation.&
& & & &Again,I refer to the official documents of Selenium-WebDriver.
.The page illustrate a test by HtmlUnitDriver .so
,I think Selenium-WebDriver is relatively independent tool.It can test web by invoke many different local browsers,which include FF,Chrome,IE and even the HtmlUnit(Although it doesn`t have a GUI).
& & A other reference comper the performance of the browers by grouping the browers into GUI browsers consisting of IE\FF\Chrome and HtmlUnit Driver.the start-up process of the former is very slowly
but &the ability of supporting &JS is considerably powerful.the latter is conversely and if the &JS has been more complicated ,the driver can`t capture the element.
& & & & & & & &
&&相关文章推荐
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:97491次
积分:2725
积分:2725
排名:第11955名
原创:172篇
转载:52篇
(2)(11)(14)(1)(1)(2)(2)(1)(3)(6)(3)(3)(10)(3)(5)(4)(3)(2)(6)(2)(4)(3)(8)(6)(2)(2)(1)(13)(9)(13)(16)(22)(4)(1)(5)(6)(17)(1)(6)(1)(1)

我要回帖

更多关于 webdriver htmlunit 的文章

 

随机推荐