“未定义不是函数"使用PhantomJS在Selenium(Java)下登录页面 [英] "undefined is not a function" on page login under Selenium (Java) using PhantomJS

查看:91
本文介绍了“未定义不是函数"使用PhantomJS在Selenium(Java)下登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java下的Selenium来设置搜寻器.

I'm trying to set up a crawler, using Selenium under Java.

当我使用ChromeDriver时,它工作正常.但是,如果尝试使用像PhantomJS这样的无头浏览器,则会遇到很多新问题.

When I use ChromeDriver it works fine. However, I get into a whole new bunch of problems if I try to use a headless browser like 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...

注意:

程序仍在运行,但是由于此错误,我无法登录页面.

Program is still running but because of this error I was unable to login into the page.

如何解决这个问题?

还有其他选择吗?

编辑一个:

在关注@Alfonso Presa之后,我尝试了此操作:

After following @Alfonso Presa I tried this:

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;

不幸的是,它现在引发此错误:

unfortunately it throws this error now :

[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版,并且我的原始代码运行良好.

after many trial, I install phantomJS version 1.9.8 and my original code just worked fine.

总的来说:

  1. phantomJS 1.9.0版有很多错误(只需多加一些错误即可)
  2. phantomJS 2.0版存在上述问题
  3. phantomJS版本1.9.8可以正常工作.我将尝试调查可能是什么问题.

推荐答案

PhantomJS与Web标准不兼容.特别适合您的情况: https://github.com/ariya/phantomjs/issues/10522

PhantomJS is known to have some incompatibilities with web standards. Specially for your case this one: https://github.com/ariya/phantomjs/issues/10522

最可能引起您问题的原因是 bind 方法正在您要爬网的网络中使用.除了PhantomJS之外,几乎所有浏览器都包含该方法.

The most probable cause for your problem is that the bind method is being used in the web you're trying to crawl. That method is included in almost all browsers but PhantomJS.

要解决您的问题,如果您有机会更改要爬网的网页,请尝试插入一个如下所示的polifyll: https://developer.mozilla.org/es /docs/Web/JavaScript/Referencia/Objetos_globales/Function/bind#Polyfill

To solve your issue, if you have the chance to change the web page you're crawling try to insert a polifyll like this one: https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Function/bind#Polyfill

如果不可能,您可以尝试使用executeScript方法注入它.

If that's not possible you can try to inject it using executeScript method.

请注意,这似乎已针对Phantom 2.0解决了.

Note that this seems to have been solved for Phantom 2.0.

这篇关于“未定义不是函数"使用PhantomJS在Selenium(Java)下登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆