问题描述
我试图了解casperjs,但为此感到挣扎。有人可以告诉我这为什么起作用(它导航到):
Im trying to understand casperjs but struggling with this. Can someone pleas tell me why this works (it navigates to http://www.w3schools.com/html/default.asp):
var casper = require('casper').create();
var mouse = require("mouse").create(casper);
casper.start('http://www.w3schools.com/');
casper.then(function(){
this.click('a.btn');
});
casper.then(function(){
console.log('Location is now: ' + this.getCurrentUrl());
});
casper.run();
但是如果我替换
this.click('a.btn');
与
this.mouse.click('a.btn');
然后它将停留在同一页面上。我以为这些都一样。
Then it stays on the same page. I thought these where the same.
推荐答案
根据CasperJS的即时测试:
According to Instant Testing with CasperJS:
这就产生了一个第二个问题,即为什么会有不同(据我所见,w3schools.com HTML非常干净,直接,没有看不见的层,也没有对点击动作的JavaScript干预)。
That creates the secondary question of why would that make a difference (the w3schools.com HTML is very clean and straightforward, as far as I can see, no invisible layers, or fancy JavaScript interventions on click actions).
事实证明原因很简单。默认视口大小非常小:您的按钮不在屏幕上,因此鼠标无法单击它!这是一个对我有用的快速测试脚本:
And the reason turned out to be very simple. The default viewport size is very small: your button was off-screen, so the mouse could not click it! Here is a quick test script that does work for me:
var casper = require('casper').create();
//var mouse = require("mouse").create(casper);
casper.options.viewportSize = {width: 1024,height: 768};
casper.start('http://www.w3schools.com/');
casper.then(function(){
this.mouse.click('a.btn');
});
casper.then(function(){
console.log('Location is now: ' + this.getCurrentUrl());
});
casper.run();
我同时使用PhantomJS和SlimerJS进行了测试。但是我只有在使用SlimerJS进行测试时才意识到该问题,并且可以看到生成的HTML。在 this.mouse.click('a.btn');之前放置
this.capture( aboutToClick.png);
code>也是一种很好的故障排除方法。
I tested with both PhantomJS and SlimerJS. But I only realized the problem when testing with SlimerJS and could see the HTML that was generated. Putting a
this.capture("aboutToClick.png");
just before your this.mouse.click('a.btn');
would also have been a good troubleshooting approach.
ASIDE:我已注释掉
var鼠标
行显示您不需要它: casper
对象内部有一个对象。
ASIDE: I've commented out the
var mouse
line to show you do not need it: a casper
object has one internally.
这篇关于无法获得“ this.mouse.click()”与casperjs合作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!