简体   繁体   English

使用casperjs单击按钮时无响应

[英]No response when clicking button with casperjs

For some reason whenever I attempt to click a button using CasperJS it never actually responds. 由于某种原因,每当我尝试使用CasperJS单击按钮时,它实际上都不会响应。 I render a screenshot and it acts as if it never clicked it at all. 我渲染了一个屏幕截图,它的表现就好像根本没有单击它一样。

phantom.casperPath = './modules/';
phantom.injectJs(phantom.casperPath +'/bin/bootstrap.js');
var system = require('system');
var utils = require('utils');
var casper = require('casper').selectXPath;

var casper = require('casper').create({
    verbose: false,
    logLevel: 'debug'
});

casper.options.viewportSize = {width: 1920, height: 1080};

casper.start();
casper.thenOpen('https://mydomain.com', function(){
});

casper.then(function() {
    this.fill('form#loginPage',{
        'user_name': 'myuser',
        'user_password': 'mypassword'
    },false);
    casper.wait(100);
});
casper.then(function(){
    this.click('#sysverb_login');
    casper.wait(100);
});
casper.then(function(){
    this.capture('test.png');
    this.echo('success');
    this.exit();
});
casper.run(function() {
});

It is not advised to use then after a page is loaded, I quote from the docs :- 不建议使用, then在页面加载后,我引用docs :-

Step functions added to then() are processed in two different cases: 添加到then()的步骤函数在两种不同情况下进行处理:

  1. When the previous step function has been executed, 执行上一步功能后,
  2. when the previous main HTTP request has been executed and the page loaded; 当先前的主要HTTP请求已执行并加载了页面时;

Note that there's no single definition of page loaded; 请注意,没有页面加载的单一定义。 is it when the DOMReady event has been triggered? 是何时触发DOMReady事件? Is it “all requests being finished”? 是“所有请求都已完成”吗? Is it *all application logic being performed”? 是“正在执行所有应用程序逻辑”吗? Or “all elements being rendered”? 还是“所有元素都被渲染”? The answer always depends on the context. 答案总是取决于上下文。 Hence why you're encouraged to always use the waitFor() family methods to keep explicit control on what you actually expect. 因此,为什么鼓励您始终使用waitFor()系列方法来保持对实际期望的显式控制。

A common trick is to use waitForSelector(): 一个常见的技巧是使用waitForSelector():

try (replace SOMESELECTOR with a selector that is visible on page after the post back happens):- 试试(用回发发生后在页面上可见的选择器替换SOMESELECTOR):-

casper.then(function() {
    this.fill('form#loginPage',{
        'user_name': 'myuser',
        'user_password': 'mypassword'
    },false);
});

casper.then(function(){
  this.click('#sysverb_login');
});

casper.waitForSelector("SOMESELECTOR", function(){
  this.capture('test.png');
  this.echo('success');
  this.exit();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM