CasperJS在浏览器控制台

时间:2016-07-16 03:37:16

标签: javascript methods javascript-events phantomjs casperjs

我正在尝试使用CasperJS调用javascript方法。 该网页只包含一个链接,允许我更改回我的默认国家/地区。

<a id="defaultCountry" href="javascript:__doPostBack('cty$UK$default','')">Default Country</a>

我希望CasperJS调用点击链接后调用的javascript方法。 我认为模仿鼠标点击链接会调用javascript方法,但它没有。 我尝试了以下方法但没有成功:

 casper.then(function() {
        casper.click(x('//*[@id="defaultCountry"]'));
        casper.evaluate(function() {
              __doPostBack('cty$UK$default',''); //this is the javascript function. im not sure if thats how you would call it though
        });

this.clickLabel('Default Country', 'a');

我知道如果我在浏览器控制台上调用javascript函数,它会起作用。我只需输入:

__doPostBack('cty$UK$default','');

在控制台中,它神奇地起作用。 任何帮助表示赞赏!

编辑:

@Rippo这是我跑步的片段。似乎CasperJS绕过了我的评估声明。最后几行直接来自控制台。由于屏幕截图,我知道我的页面会加载选择器。我甚至使用了casper.waitforselector方法来确认。

casper.thenOpen('http://example.com');

casper.wait(5000, function() {
    console.log('page opened');
    casper.capture('page.png');
    console.log('capture page complete');
});

casper.thenEvaluate(function() {
    console.log('invoking javascript');
    __doPostBack('cty$UK$default','');
    console.log('javascript invoked');
});

这是来自控制台:

[info] [phantom] wait() finished waiting for 5000ms.
page opened
[debug] [phantom] Capturing page to C:/Users/page.png
[info] [phantom] Capture saved to C:/Users/page.png
capture page complete
[info] [phantom] Step _step 8/8 http://example.com (HTTP 200)
[info] [phantom] Step _step 8/8: done in 16240ms.
[info] [phantom] Done 8 steps in 16259ms
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "about:blank"

这是退出前控制台的最后一行。

编辑7/17/2016 12:40 AM

@Artjom B. 以下是我根据您的建议运行的代码片段:

...snipped some prior    
function() {
    console.log('Page loaded');
    casper.capture('page.png');
    console.log('Starting 1st postback call');
    this.evaluate(function() {
        console.log('postback call');
         __doPostBack('cty$UK$default','');
        });
    console.log('passed postback');
    casper.capture('post-postback.png');

//At this point, it skips this function and goes straight to console.log then comes back to this function. Not sure why...
casper.then(function() {
    console.log('trying to change again');
    this.click(x('//*[@id="defaultCountry"]'));
    this.evaluate(function() {
        __doPostBack('cty$UK$default','');
        console.log('javascript invoked');
    });
});

//skipped to this console.log
console.log('waiting country to change');
this.waitForSelector('.countryuk', 
    function() {
        console.log('country change completed. Capturing image');
        this.capture('uk.png');
    },

    function() {
        console.log('timed out waiting for country to change.');
        this.capture('uk-timeout.png');
    },5000);

};

这是控制台输出:

Page loaded
[debug] [phantom] Capturing page to C:/Users/page.png
[info] [phantom] Capture saved to C:/Users/page.png
Starting 1st postback call
Console: postback call
Error: ReferenceError: Can't find variable: __doPostBack
passed postback
[debug] [phantom] Capturing page to C:/Users/post-postback.png
[info] [phantom] Capture saved to C:/Users/post-postback.png
waiting country to change
[info] [phantom] Step anonymous 10/11 http://example.com/page.aspx?r=2 (HTTP 200)
trying to change again
[debug] [phantom] Mouse event 'mousedown' on selector: xpath selector: //*[@id="defaultCountry"]
[debug] [phantom] Mouse event 'mouseup' on selector: xpath selector: //*[@id="defaultCountry"]
[debug] [phantom] Mouse event 'click' on selector: xpath selector: //*[@id="defaultCountry"]
Error: ReferenceError: Can't find variable: __doPostBack
Error: ReferenceError: Can't find variable: __doPostBack
[info] [phantom] Step anonymous 10/11: done in 22567ms.
[info] [phantom] Step _step 11/11 http://example.com/page.aspx?r=2 (HTTP 200)
[info] [phantom] Step _step 11/11: done in 22573ms.
[warning] [phantom] Casper.waitFor() timeout
timed out waiting for country to change.
[debug] [phantom] Capturing page to C:/Users/uk-timeout.png
[info] [phantom] Capture saved to C:/Users/uk-timeout.png
[info] [phantom] Done 11 steps in 27825ms
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "about:blank"

显然它无法找到回发功能。我不知道为什么会这样。它不是一个隐藏的元素。它与我上面发布的完全相同(链接)。它嵌套在一堆div标签中,但它是关于它的。谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

如何(删除casper.thencasper.click

 casper.thenEvaluate(function() {
       __doPostBack('cty$UK$default',''); 
 });

答案 1 :(得分:0)

您可以访问链接的href并使用eval功能。

var str = document.getElementById("defaultCountry").href;
eval(str.substring(str.indexOf(":") + 1));