无法获取" this.mouse.click()"与casperjs合作 [英] Cant get "this.mouse.click()" to work with casperjs
问题描述
我试图了解casperjs,但为此感到挣扎.有人可以告诉我这为什么起作用(它导航到 http://www.w3schools.com/html/default.asp ):
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:
casper.click()
创建一个事件并将其分配给目标事件,但casper.mouse.click()
不处理任何元素,但只是在给定位置产生鼠标动作.
casper.click()
creates an event and dispatches it to the targeted event, butcasper.mouse.click()
does not deal with any element but just produces a mouse action at the given position.
这产生了一个第二个问题,即为什么会有不同(据我所知,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.capture("aboutToClick.png");
放在您的 this.mouse.click('a.btn');
之前也可以很好地解决问题方法.
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.
助手:我已经注释掉 var mouse
行以表明您不需要它:一个 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合作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!