我正在尝试设置一个爬虫,在Java下使用Selenium。
当我使用ChromeDriver时,它可以正常工作。但是,如果我尝试使用像PhantomJS这样的无头浏览器,我会遇到一大堆问题。
这就是问题发生的地方:
Capabilities caps = new DesiredCapabilities();
((DesiredCapabilities) caps).setJavascriptEnabled(true);
((DesiredCapabilities) caps).setCapability("takesScreenshot", false);
((DesiredCapabilities) caps).setCapability("locationContextEnabled", true);
((DesiredCapabilities) caps).setCapability("acceptSslCerts", true);
PhantomJSDriver jsDriver = new PhantomJSDriver(caps);
driver = jsDriver;
System.out.println("driver is set...");
driver.get("http://localhost/login"); //erorrs appears to be here
System.out.println("gonna wait for page loading...");
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.elementToBeClickable(By.id("emailAddr")));
WebElement element_email = driver.findElement(By.id("emailAddr"));
element_email.clear();
element_email.sendKeys("example@example.com");
WebElement element_password = driver.findElement(By.id("password"));
element_password.clear();
element_password.sendKeys("password");
WebElement element_login = driver.findElement(By.xpath("//input[@type=\"submit\"]"));
element_login.click();
我得到的错误
driver is set...
[ERROR - 2015-08-27T11:45:32.378Z] Session [1c04b0e0-4cb1-11e5-aef3-a75ceb7c4223] - page.onError - msg: TypeError: undefined is not a function (evaluating '$("#location-search-box").geocomplete')
:262 in error
[ERROR - 2015-08-27T11:45:32.379Z] Session [1c04b0e0-4cb1-11e5-aef3-a75ceb7c4223] - page.onError - stack:
init (http://localhost/static/js/auto/537c5ede-d569-491d-bd95-4916c763f9cf.js:9407)
(anonymous function) (http://localhost/static/js/auto/537c5ede-d569-491d-bd95-4916c763f9cf.js:11336)
l (http://localhost/static/js/jquery.min.js:2)
fireWith (http://localhost/static/js/jquery.min.js:2)
ready (http://localhost/static/js/jquery.min.js:2)
A (http://localhost/static/js/jquery.min.js:2)
:262 in error
gonna wait for page loading...
注:
程序仍在运行,但由于此错误,我无法登录该页面。
如何解决这个问题?
还有其他选择吗?
编辑一:在关注@Alfonso Presa后,我尝试了这个:
String polyfill = "" +
" if (!Function.prototype.bind) {" +
" Function.prototype.bind = function(oThis) {" +
" if (typeof this !== 'function') {" +
" // closest thing possible to the ECMAScript 5" +
" // internal IsCallable function" +
" throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');" +
" }" +
"" +
" var aArgs = Array.prototype.slice.call(arguments, 1)," +
" fToBind = this," +
" fNOP = function() {}," +
" fBound = function() {" +
" return fToBind.apply(this instanceof fNOP && oThis" +
" ? this" +
" : oThis," +
" aArgs.concat(Array.prototype.slice.call(arguments)));" +
" };" +
"" +
" fNOP.prototype = this.prototype;" +
" fBound.prototype = new fNOP();" +
"" +
" return fBound;" +
" };" +
" }" ;
PhantomJSDriver jsDriver = new PhantomJSDriver(capabilities);
jsDriver.executeAsyncScript(polyfill);
driver = jsDriver;
不幸的是它现在抛出了这个错误:
[ERROR - 2015-08-27T13:05:25.354Z] Session [482dc570-4cbc-11e5-960c-81dcdf4848bd] - page.onError - msg: SyntaxError: Unexpected token ')'
:262 in error
[ERROR - 2015-08-27T13:05:25.354Z] Session [482dc570-4cbc-11e5-960c-81dcdf4848bd] - page.onError - stack:
Function (undefined:2)
Na (phantomjs://webpage.evaluate():14)
(anonymous function) (phantomjs://webpage.evaluate():15)
(anonymous function) (phantomjs://webpage.evaluate():15)
(anonymous function) (phantomjs://webpage.evaluate():16)
(anonymous function) (phantomjs://webpage.evaluate():16)
:262 in error
编辑二:
经过多次试用,我安装了phantomJS 1.9.8版,我的原始代码运行正常。总体来说:
答案 0 :(得分:1)
众所周知,PhantomJS与Web标准存在一些不兼容性。特别针对您的情况:https://github.com/ariya/phantomjs/issues/10522
问题的最可能原因是您正在尝试抓取的网络中使用了bind
方法。除了PhantomJS之外,几乎所有浏览器都包含该方法。
要解决您的问题,如果您有机会更改您正在抓取的网页,请尝试插入类似以下的polifyll: https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Function/bind#Polyfill
如果不可能,您可以尝试使用executeScript
方法注入它。
请注意,Phantom 2.0似乎已经解决了这个问题。