0
var Xray = require('x-ray');
    var x = Xray();
    var a = false;


    var url = "abcdeabcde";

            x(url, '.listing_risultati_prodotti .smcc-listing-risultati-prodotto', [{
                title: '.first-col  a',
                link: '.product-description a@href',
                prezzo: '.second-col .ish-priceContainer-salePrice'
            }])(function(err, obj) {
                console.log(obj)   
            })

The query right now return two times the same value because there are two div that match the query.

[ { title: 'aaaa',
    link: 'aaaa',
    prezzo: 'aaaa' },
  { title: 'bbb',
    link: 'bbb',
    prezzo: ' bbb' },
  { title: 'ccc',
    link: 'ccc',
    prezzo: 'ccc' },
.....
....
.....
Then again
{ title: 'aaaa',
    link: 'aaaa',
    prezzo: 'aaaa' },
  { title: 'bbb',
    link: 'bbb',
    prezzo: ' bbb' },
  { title: 'ccc',
    link: 'ccc',
    prezzo: 'ccc' }]

both div have same selector path

#maincontent > div.category-section > div.render-category-products.products > div.listing_risultati_prodotti 

They are both nested inside this id: #smcc_comp_common_wrapper

structure is like:

<body>
#smcc_comp_common_wrapper
...
...
#mainwrapper
    ...
    #maincontent > div.category-section > div.render-category-products.products > div.listing_risultati_prodotti 
    ...
#mainwrapper
    ...
    #maincontent > div.category-section > div.render-category-products.products > div.listing_risultati_prodotti 
    ...
...
...

i'm trying something like:

x(url, '.listing_risultati_prodotti:nth-of-type(1) .smcc-listing-risultati-prodotto',

or also

x(url, ':nth-match(1 of #mainwrapper) .listing_risultati_prodotti .smcc-listing-risultati-prodotto', but no one works

Is possible targeting only the first class instance?

Cœur
  • 37,241
  • 25
  • 195
  • 267
enF
  • 59
  • 1
  • 8

2 Answers2

0

If any solution which uses selectors (e.g. using :first-child as Vaibhav suggested) fails, how about just slicing the resultant object array in half?

function(err, obj) {
    var half = obj.splice(obj.length/2);
}

Note, that it is a kind of a workaround for your problem, however it may be acceptable in that particular case.

  • if i don't find other solution i'm gonna use this one, but like you said is a workaround – enF Aug 09 '16 at 22:39
0

it is:

x(html, 'div#smcc_comp_common_wrapper div#mainwrapper:first-of-type div.listing_risultati_prodotti'
    )(function(err, obj) {
        console.log(obj) 
    })

also there should not be multiple elements with the same id

frustrum
  • 341
  • 3
  • 7