Search code examples
javascriptcasperjs

Extract text from a DOM element in javascript casperjs


for(var i=0; i < 20; i++) { 

this.fetchText('div.ma-AdCardMyAds:nth-child('+i+') > div.one > div.two > div.three > span[class="ma-AdCard-price"]'));

}

There are occasions that before span there are 2 divs instead of 3 divs. I need to always reach span[class="ma-AdCard-price"] starting from div.ma-AdCardMyAds:nth-child('+1+')

Only div.one > div.three

this.fetchText('div.ma-AdCardMyAds:nth-child('+i+') > div.one > div.three > span[class="ma-AdCard-price"]'));

doesn't work

this.fetchText('div.ma-AdCardMyAds:nth-child('+i+') > span[class="ma-AdCard-price"]')

Solution

  • You can try this:

    this.fetchText('div.ma-AdCardMyAds:nth-child('+i+') span[class="ma-AdCard-price"]')
    

    And if div.one and div.two are always there, use:

    this.fetchText('div.ma-AdCardMyAds:nth-child('+i+') > div.one > div.two span[class="ma-AdCard-price"]')