使用Angular js抓取网站

时间:2016-11-24 20:13:58

标签: angularjs casperjs

我正在尝试抓一个使用AngularJs的网站,页面上有四个单选按钮,其中两个有'ng-required = true'。选择了所有四个按钮,但是当我单击提交按钮时,即使选择了按钮,它也会在单击按钮上出现错误,这些按钮的要求为真。我拍了截图,甚至在屏幕截图中显示了选中的单选按钮。我试图覆盖验证但没有任何效果。请帮助这里是Html和代码

HTML 这是公共服务单选按钮

<input id="publicServiceTrue" type="radio" value="false"
   name="ibrComposite.application.pslfIndicator"
   ng-model="pslf"  target="dependents_section_div"
   ng-required="true"/>

婚姻状况单选按钮

<input id="maritalStatusSingle" type="radio" value="SINGLE"
name="ibrComposite.application.maritalStatus"
ng-model="maritalStatus" ng-required="true" />

// Casper Code

casper.then(function () {   
    this.evaluate(function() {
        $('#applicationReason_NEW').prop("checked", true);
    });

    this.evaluate(function() {

        $('publicServiceTrue').prop("checked", true); // This radio button shows error even when it is selected
    });

    this.evaluate(function(){

    $('#applicationNbChildren').val('0').change();
    $('#applicationNbDep').val('0').change();
    });
    casper.wait(800, function(){
        this.capture('IRS/Third_button.png')
    });



    this.evaluate(function() {
        $('#maritalStatusSingle').prop("checked", true); // This radio button shows error even when it is selected


    });
    casper.wait(1000, function(){
        this.capture('IRS/Fourth_button.png')
    });
    document.querySelectorAll("input[type='submit']")[0].click();
});

1 个答案:

答案 0 :(得分:1)

this方法中的.then关键字未绑定到父this。而是使用胖箭头=>语法。

//casper.then(function () {
//Use fat arrow syntax   
casper.then( () => {   
    this.evaluate(function() {
        $('#applicationReason_NEW').prop("checked", true);
    });

    this.evaluate(function() {

        $('publicServiceTrue').prop("checked", true); // This radio button shows error even when it is selected
    });

或明确绑定父级中的this关键字:

var that = this;
casper.then( function() {   
    //this.evaluate(function() {
    that.evaluate(function() {
        $('#applicationReason_NEW').prop("checked", true);
    });

    //this.evaluate(function() {
    that.evaluate(function() {

        $('publicServiceTrue').prop("checked", true); // This radio button shows error even when it is selected
    });

来自Promises/A+ Spec

  

3.3也就是说,在严格模式下this将成为undefined(成功/拒绝处理程序);在草率模式下,它将成为全局对象。