简体   繁体   English

使用Casper.js单击按钮

[英]Using Casper.js to click on buttons

I am working on a project to scrape data from an e-commerce website. 我正在做一个从电子商务网站抓取数据的项目。 I am trying to implement it using Casper.js. 我正在尝试使用Casper.js来实现它。 I am trying to iterate through a Nodelist of AngularMaterial buttons, clicking on them will navigate to a new page/state - my objective is to grab the URL generated. 我试图遍历AngularMaterial按钮的Nodelist,单击它们将导航到新页面/状态-我的目标是获取生成的URL。 I am having struggling with properly firing the click event on buttons and doing it iteratively. 我正在努力正确触发按钮上的click事件并进行迭代。 The following is what I got so far. 以下是我到目前为止所得到的。

casper.start( "http://store.com", function( ) {
    this.echo(this.getTitle( ) );
} );

casper.waitForSelector( '.md-card-image', function( ) { //Going to Home page and waiting for AJAX content to load
    console.log( 'AJAX results loaded' );
    this.echo( this.getTitle( ) );
    var product = this.evaluate( function( ) {
        return document.querySelector('html').outerHTML;

    } ) 

} )



function goToDetails( ) { //Using querySelector to target the buttons I want to nav to.
   var products = document.querySelectorAll( 'button.md-primary.md-button.md-hue-2.md-raised.md-ink-ripple' );

   return products;

}

casper.then( function( ) { //Trying to access the first button, no luck =[
details = this.evaluate( goToDetails );

this.echo( "Second test" );
console.log(details.length);  //length of 30 - for 30 buttons.
  if(casper.exists(  "button.md-primary.md-button.md-hue-2.md-raised.md-ink-ripple"  )){
      this.echo('found')
  }

  this.clickLabel('Details')

  })

});
casper.then(function(){
    casper.waitForSelector('.image-large-preview', function(){
        casper.echo(casper.getCurrentUrl()); //im getting the proper URL here.
    })
    this.back(); // going back to the home page
})

This method clicks the first available button, grabs the URL that it gets navigated to, and goes back to the main page. 此方法单击第一个可用按钮,获取其导航到的URL,然后返回主页。 I need to find a way to click on all the buttons one-by-one and grab the URL. 我需要找到一种方法来一个接一个地单击所有按钮并获取URL。 Currently trying to find the correct way to target the buttons, This is the markup for one of the cards ( out of 30 on the page). 当前正在尝试找到定位按钮的正确方法,这是其中一张卡的标记(页面上30条标记)。 I am trying to target the button with the class "class="md-primary md-hue-2 md-raised md-button md-ink-ripple". 我正在尝试使用“ class =“ md-primary md-hue-2 md-raised md-button md-ink-ripple”类定位按钮。

   <div flex="33" flex-sm="50" flex-xs="50" layout="row" ng-repeat="product in resultsCtrl.products" class="layout-row flex-xs-50 flex-sm-50 flex-33" style="">
        <md-card flex="" ng-click="resultsCtrl.goToDetail( product )" ng-class="{ 'md-whiteframe-3dp': hover }" ng-mouseenter="hover = true" ng-mouseleave="hover = false" class="_md flex" role="button" tabindex="0">
            <img  class="md-card-image" style="padding-top:10px">
            <div layout="row" layout-align="end center" ng-init="condition = resultsCtrl.getCondition( product )" class="label-row layout-align-end-center layout-row">
                <div ng-show="condition.isVisible" ng-class="{ grey: condition.labelColor,  tiny: resultsCtrl.$mdMedia( 'xs' ) }" class="ui label ng-hide grey" aria-hidden="true">New</div>
            </div>
            <md-card-title>

            </md-card-title>
            <md-card-content>
                <div layout="row" layout-align="end end" class="price-sizing layout-align-end-end layout-row">
                    <div class="msrp results-msrp">

                        <span class="value price-strikethrough">
                        $249.99
                        </span>
                    </div>
                    <!----><div ng-if="product.products[0].retailPrice.amount">
                        <div>
                            <span class="ui tiny statistic">
                                <span class="value">
                                $79.99
                                </span>
                            </span>
                        </div>
                    </div><!---->
                </div>

            </md-card-content>
            <md-card-actions layout="column" class="layout-column">
                <button class="md-primary md-hue-2 md-raised md-button md-ink-ripple" type="button" ng-transclude="" ng-click="resultsCtrl.goToDetail( product )"><span>Details</span></button>
            </md-card-actions>
        </md-card>
    </div>

Not really shure where's the Problem, but for the CLick all Button Problem this should help: 并不是很清楚问题出在哪里,但是对于单击全部按钮问题,这应该有所帮助:

...

var buttons = casper.getElementsAttribute("button.md-primary.md-button.md-hue-2.md-raised.md-ink-ripple", 'id');
while (buttons.length > 0) {
    button = buttons.pop();
    (function(button) {
        casper.then(function() {
            casper.echo("Button id is " + button);
        });
        casper.then(function() {
            casper.click("#" + button);
        });
        casper.waitForSelector('.selector', function() {
            casper.echo(casper.getCurrentUrl());
        });
    })(button);
}

...

Another issue could be that the Ajax stuff is not loaded completely. 另一个问题可能是Ajax内容未完全加载。 Just try it with a static wait or waitForResource. 只需尝试使用静态wait或waitForResource。
Sometimes it makes sence to activate an Ajax-Container to load the data. 有时需要激活Ajax容器来加载数据。

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

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