用PhantomJS注入Javascript的正确语法是什么?

What is the right syntax for injecting Javascript with PhantomJS?

本文关键字:语法 是什么 PhantomJS 注入 Javascript      更新时间:2023-09-26

我的目标是:

javascript document.getElementById("pickupZip").value = "90049";
document.getElementById("refreshStoresForZipPop").click();
用PhantomJS把

放到一个网站中。这是我的inject.js文件上的所有代码,我在控制台:SyntaxError: Expected an identifier but found "document" instead中得到它,所以我知道它的语法是错误的,尽管我找不到一个显示正确的地方。下面是我的PhantomJS文件代码:

var webPage = require('webpage');
var page = webPage.create();
page.open('http://shop.advanceautoparts.com/p/purolator-classic-air-filter-a24278/5792304-P?navigationPath=L1*14934/', function(status) {
    var success = phantom.injectJs('inject.js');
    console.log(success);
    if (status === "success") {                   
      page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function() {
            page.render('advanceautoparts.png');
            phantom.exit();
        });
    }
});

使用evaluate

var webPage = require('webpage');
var page = webPage.create();
page.open('http://shop.advanceautoparts.com/p/purolator-classic-air-filter-a24278/5792304-P?navigationPath=L1*14934/', function(status) {
    var success = phantom.injectJs('inject.js');
    console.log(success);
    if (status === "success") {                   
       page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function() {
            page.evaluate(function(s) {
                document.getElementById("pickupZip").value = "90049";
                document.getElementById("refreshStoresForZipPop").click();
              });
            page.render('advanceautoparts.png');
            phantom.exit();
        });
    }
});

参考:http://phantomjs.org/api/webpage/method/evaluate.html