PhantomJS按钮单击和表单提交

时间:2016-05-22 00:17:08

标签: javascript phantomjs

我对PhantomJS很陌生,所以我只想偶然发现一些实际的例子(文档对这些例子略有启发)。

我尝试的一件事是投票给一个虚假的民意调查民意调查,但我正在努力。使用下面的脚本,我可以从第一个屏幕截图中看出我的选项按钮被点击/选中。但为什么我的答案没有提交?尽管我的代码在第二个click的VOTE按钮上执行evaluate事件,但第二个屏幕截图看起来与第一个相同。谁知道为什么?知道如何使其发挥作用吗?

var webPage = require('webpage');
var page = webPage.create();

//******* BEGIN LOGGING METHODS *******    
// http://phantomjs.org/api/webpage/handler/on-url-changed.html
page.onUrlChanged = function(targetUrl) {
  console.log('New URL: ' + targetUrl);
};

// http://phantomjs.org/api/webpage/handler/on-console-message.html
page.onConsoleMessage = function(msg, lineNum, sourceId) {
  console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};

// http://phantomjs.org/api/webpage/handler/on-error.html
page.onError = function(msg, trace) {
  var msgStack = ['ERROR: ' + msg];
  if (trace && trace.length) {
    msgStack.push('TRACE:');
    trace.forEach(function(t) {
      msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
    });
  }
  console.error(msgStack.join('\n'));
};

// http://phantomjs.org/api/webpage/handler/on-resource-error.html
page.onResourceError = function(resourceError) {
  console.log('Unable to load resource (#' + resourceError.id + ' URL:' + resourceError.url + ')');
  console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};

// http://phantomjs.org/api/webpage/handler/on-resource-timeout.html
page.onResourceTimeout = function(request) {
    console.log('Response Timeout (#' + request.id + '): ' + JSON.stringify(request));
};
//******* END LOGGING METHODS *******    

page.open('https://polldaddy.com/poll/9424638/', function(status) {
    console.log('Status: ' + status);

    //make selection    
    var myselection = page.evaluate(function() {
        var ev = document.createEvent("MouseEvent");
        ev.initMouseEvent("click", true, true, window, null,0, 0, 0, 0, false, false, false, false, 0, null);

        //radio button for Curly has id=PDI_answer42988707
        var myselection = document.querySelector("#PDI_answer42988707");
        (myselection).dispatchEvent(ev);
        return document.title;
    });
    //screen capture the selection
    page.render('selection.png');
    console.log(myselection);

    //click the Vote button
    var dovote = page.evaluate(function() {
        var ev = document.createEvent("MouseEvent");
        ev.initMouseEvent("click", true, true, window, null,0, 0, 0, 0, false, false, false, false, 0, null);

        //get a handle to the vote button
        var votebutton = document.querySelector("a.vote-button");
        //click the vote button
        (votebutton).dispatchEvent(ev);
        return document.title;
    });
    //delay, then take screenshot...
    setTimeout(
        function(){
            page.render('voted.png');
            console.log(dovote);
        }
        ,2000
    );

});

我在Linux Mint上使用PhantomJS 1.9.0执行脚本。执行参数类似于克服任何SSL握手失败:

phantomjs --ignore-ssl-errors=true --ssl-protocol=any myscript.js

我也尝试在命令行上设置--cookies-file--local-storage-path,但这也无济于事。

我从上面所有控制台记录中得到的输出是:

New URL: https://polldaddy.com/poll/9424638/  <-- from urlChange
CONSOLE: JQMIGRATE: Logging is active (from line # in "") <-- from onConsoleMessage
Status: success
Who is your favorite Stooge (poll 9424638) | Polldaddy.com
Who is your favorite Stooge (poll 9424638) | Polldaddy.com

我无用的民意调查在这里:https://polldaddy.com/poll/9424638/,你会从我的剧本中看到我正在尝试为Curly堆叠结果。

1 个答案:

答案 0 :(得分:0)

如果您查看polldaddy页面中包含的javascript,您将通过以下检查找到投票功能:

if (event.pageX) {
  eventX = event.pageX;
  eventY = event.pageY;
} else {
  eventX = event.clientX;
  eventY = event.clientY;
}

...

if (eventX == 0 && eventY == 0) {
  return false
}

如果x和y位置都等于0,基本上他们正在查看事件位置和短路。

如果更改了您创建的鼠标事件,以便clientX或clientY值不为0,那么您的脚本应该可以正常工作。