gpt4 book ai didi

javascript - CasperJS 在浏览器控制台上调用 Javascript 函数

转载 作者:行者123 更新时间:2023-11-27 22:46:07 25 4
gpt4 key购买 nike

我正在尝试使用 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 似乎绕过了我的 eval 语句。最后几行直接来自控制台。由于屏幕截图,我知道我的页面加载了选择器。我什至使用了 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"

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

<小时/>

编辑 2016 年 7 月 17 日 12:40

@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 标签中,但仅此而已。

最佳答案

怎么样(删除 casper.thencasper.click)

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

关于javascript - CasperJS 在浏览器控制台上调用 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38407463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com