无法获得“ this.mouse.click()”与casperjs合作 [英] Cant get "this.mouse.click()" to work with casperjs

查看:85
本文介绍了无法获得“ this.mouse.click()”与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, but casper.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.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合作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆