Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get last OPTION from SELECT list using XPath - Scrapy

Tags:

python

scrapy

I am using this selector but it is giving error

//*[@id="quantity"]/option/[last()-1]

How do I select last OPTION?

I am using Scrapy Framework.

like image 785
Umair Ayub Avatar asked Dec 19 '22 13:12

Umair Ayub


1 Answers

You have an extra / before the [ making the XPath expression invalid. Remove it:

//*[@id="quantity"]/option[last()-1]

Note that you can also solve it using Python/Scrapy:

response.xpath('//*[@id="quantity"]/option')[-1].extract()

Or, in a CSS selector form:

response.css('#quantity option:last-child').extract_first()
response.css('#quantity option')[-1].extract()
like image 86
alecxe Avatar answered Mar 05 '23 21:03

alecxe