Issue
I tring to parse text from span tag. When I indexing div, like in the example below, it works. When I try index over span tag, it does not work. Output of span and span[1] is the same, and output of span[2+] is empty list. Could anybody help how to iterate over span selectors?
In [74]: response.xpath('//custom-dropdown[@id="bodytypes"]/div[2]/label/span')
Out[74]:
[<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span' data='<span class="sc-ellipsis">Compact</span>'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span' data='<span class="sc-ellipsis">Convertible</s'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span' data='<span class="sc-ellipsis">Coupe</span>'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span' data='<span class="sc-ellipsis">SUV/Off-Road</'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span' data='<span class="sc-ellipsis">Sedans</span>'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span' data='<span class="sc-ellipsis">Station wagon<'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span' data='<span class="sc-ellipsis">Transporter</s'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span' data='<span class="sc-ellipsis">Van</span>'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span' data='<span class="sc-ellipsis">Other</span>'>]
In [75]: response.xpath('//custom-dropdown[@id="bodytypes"]/div[2]/label/span[1]')
Out[75]:
[<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span[1]' data='<span class="sc-ellipsis">Compact</span>'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span[1]' data='<span class="sc-ellipsis">Convertible</s'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span[1]' data='<span class="sc-ellipsis">Coupe</span>'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span[1]' data='<span class="sc-ellipsis">SUV/Off-Road</'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span[1]' data='<span class="sc-ellipsis">Sedans</span>'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span[1]' data='<span class="sc-ellipsis">Station wagon<'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span[1]' data='<span class="sc-ellipsis">Transporter</s'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span[1]' data='<span class="sc-ellipsis">Van</span>'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span[1]' data='<span class="sc-ellipsis">Other</span>'>]
In [76]: response.xpath('//custom-dropdown[@id="bodytypes"]/div[2]/label/span[2]')
Out[76]: []
Solution
My mistake. I just realized there is more labels and in each label is one span. So I have to iterate over label, see below:
In [98]: response.xpath('//custom-dropdown[@id="bodytypes"]/div[2]/label/span')
Out[98]:
[<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span' data='<span class="sc-ellipsis">Compact</span>'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span' data='<span class="sc-ellipsis">Convertible</s'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span' data='<span class="sc-ellipsis">Coupe</span>'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span' data='<span class="sc-ellipsis">SUV/Off-Road</'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span' data='<span class="sc-ellipsis">Sedans</span>'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span' data='<span class="sc-ellipsis">Station wagon<'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span' data='<span class="sc-ellipsis">Transporter</s'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span' data='<span class="sc-ellipsis">Van</span>'>,
<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label/span' data='<span class="sc-ellipsis">Other</span>'>]
In [99]: response.xpath('//custom-dropdown[@id="bodytypes"]/div[2]/label[1]/span')
Out[99]: [<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label[1]/span' data='<span class="sc-ellipsis">Compact</span>'>]
In [100]: response.xpath('//custom-dropdown[@id="bodytypes"]/div[2]/label[2]/span')
Out[100]: [<Selector xpath='//custom-dropdown[@id="bodytypes"]/div[2]/label[2]/span' data='<span class="sc-ellipsis">Convertible</s'>]
Answered By - dorinand
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.