CasperJS无法点击DIV按钮 [英] CasperJS cannot click on a DIV button

查看:172
本文介绍了CasperJS无法点击DIV按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用CasperJS开展一个网站抓取项目。它是一个ASPX网站。我可以登录到网站,然后填写运行搜索的表单,但在填写完表单后,我无法模拟点击DIV按钮。搜索运行使用AJAX,但是当我在等待几秒钟后捕获页面时,它不会在捕获的图像中显示结果。搜索按钮由DIV构成,点击后它会运行其他隐藏的JavaScript函数,它们发送AJAX请求来检索搜索结果。



这是我的代码。

  var casper = require('casper')。create({
clientScripts:['js / jquery-1.11.3。 min.js']
});

var x = require('casper')。selectXPath;

casper.userAgent('Mozilla / 5.0(compatible; MSIE 8.0; Windows NT 6.1; Trident / 4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en -我们)');

casper.start('https://example.com/finder.aspx');
$ b casper.then(function(){
this.sendKeys('#UserName','xxxxx');
this.sendKeys('#Password','yyyyy' );
console.log('Fill login credentials');
casper.capture(screen1.png);

this.thenClick('input [type = submit ]');
});

casper.wait(3000,function(){
console.log('Login');
casper.capture(screen2.png);
});
$ b casper.then(function(){
this.fill('form [name =searchForm]',{
'$ ddType':'ABCD',
'$ rbGender':'0',
},true);
});
$ b casper.wait(3000,function(){
this.sendKeys('#Type_I','ABCD');
this.sendKeys('#ProviderId',' ('#txtNameLast','Name');
this.sendKeys('#txtNameFirst','My');
this.sendKeys('#txtNameLast'
this.sendKeys('#txtBirthDate','01 / 11/1987');
console.log('Form filled');
casper。 capture(screen3.png);
});

casper.thenClick('#btnSubmit',function(){
console.log('Submitted');
});

casper.wait(3000,function(){
casper.capture(screen4.png);
});

这是需要点击的按钮DIV。

 < div id =btnSubmitclass =dxb> 
< span>提交< / span>
< / div>

screen4.png(与screen3.png相同)只显示填充的搜索表单,但不显示 Loading ..消息(应该在提交点击时显示)或任何结果。我怎样才能触发表单提交?
正常表单提交不会检索搜索结果。






PS:
我试过以下在 evaluate()中触发提交按钮的方法。



在文本框中输入press显示结果)

方法1



  var e = jQuery。事件( 的keydown); 
e.which = 13;
$(#txtZipCode)。trigger(e);



方法2



  this.sendKeys('#txtZipCode',casper.page.event.key.Enter,{keepFocus:true}); 

点击DIV按钮。



方法1



  $('#btnSubmit div input')。trigger('click'); 



方法2



  $( '#btnSubmit按钮')点击()。 



方法3



  var mouse = require(mouse)。create(casper); 
this.mouse.click(#btnSubmit);

仍然没有好运。

解决方法

casper.then(function(){
this.clickLabel('Submit','span');
});



我碰到类似的问题,这对我有用。


I have been working on a website scraping project using CasperJS. It is an ASPX website. I could login to the site and then fill the form that runs a search but after filling the form I cannot simulate a click on the DIV button. The search runs using AJAX but when I capture page after waiting few seconds it does not show results in the captured image. The Search button is made of DIV and upon clicking it runs other hidden JavaScript functions that send the AJAX request to retrieve the search results.

Here is my code.

var casper = require('casper').create({
    clientScripts: ['js/jquery-1.11.3.min.js']
});

var x = require('casper').selectXPath;

casper.userAgent('Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en-US)');

casper.start('https://example.com/finder.aspx');

casper.then(function () {
    this.sendKeys('#UserName', 'xxxxx');
    this.sendKeys('#Password', 'yyyyy');
    console.log('Fill login credentials');
    casper.capture("screen1.png");

    this.thenClick('input[type=submit]');
});

casper.wait(3000, function () {
    console.log('Login');
    casper.capture("screen2.png");
});

casper.then(function () {
    this.fill('form[name="searchForm"]', {
        '$ddType': 'ABCD',
        '$rbGender': '0',
    }, true);
});

casper.wait(3000, function () {
    this.sendKeys('#Type_I', 'ABCD');
    this.sendKeys('#ProviderId', '1111111111');
    this.sendKeys('#txtNameFirst', 'My');
    this.sendKeys('#txtNameLast', 'Name');
    this.sendKeys('#txtZipCode', '11111');
    this.sendKeys('#txtBirthDate', '01/11/1987');
    console.log('Form filled');
    casper.capture("screen3.png");
});

casper.thenClick('#btnSubmit', function () {
    console.log('Submitted');
});

casper.wait(3000, function () {
    casper.capture("screen4.png");
});

This is the button DIV that needs a click.

<div id="btnSubmit" class="dxb">
    <span>Submit</span>
</div>

The screen4.png (as same as screen3.png) shows only a filled search form but no "Loading.." message (which supposed to be shown upon submit click) or any result. How can I trigger the form submit? Normal form submit does not retrieve the search results.


PS: I tried the following methods to trigger the submit button inside evaluate().

Enter press while in a text box (which in reality shows the result)

Method 1

var e = jQuery.Event("keydown");
e.which = 13;
$("#txtZipCode").trigger(e);

Method 2

this.sendKeys('#txtZipCode', casper.page.event.key.Enter, {keepFocus: true});

Clicking on the DIV button.

Method 1

$('#btnSubmit div input').trigger('click');

Method 2

$('#btnSubmit').click();

Method 3

var mouse = require("mouse").create(casper);
this.mouse.click("#btnSubmit");

Still no luck.

解决方案

casper.then( function () { this.clickLabel('Submit', 'span'); });

I came across a similar issue and this worked for me.

这篇关于CasperJS无法点击DIV按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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