Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Workaround bug in lxml for multiple select options

Daniel Graña29d6bcf0d10f
diff --git a/scrapy/http/request/form.py b/scrapy/http/request/form.py
index aa23eaec7..9f6897280 100644
--- a/scrapy/http/request/form.py
+++ b/scrapy/http/request/form.py
@@ -93,16 +93,25 @@ def _get_inputs(form, formdata, dont_click, clickdata, response):
def _value(ele):
n = ele.name
v = ele.value
- # Match browser behaviour on simple select tag without options selected
- # Or for select tags wihout options
- if v is None and ele.tag == 'select' and not ele.multiple:
+ if ele.tag == 'select':
+ return _select_value(ele, n, v)
+ return n, v
+
+def _select_value(ele, n, v):
+ multiple = ele.multiple
+ if v is None and not multiple:
+ # Match browser behaviour on simple select tag without options selected
+ # And for select tags wihout options
o = ele.value_options
- if o:
- return n, o[0]
- else:
- return None, None
+ return (n, o[0]) if o else (None, None)
+ elif v is not None and multiple:
+ # This is a workround to bug in lxml fixed 2.3.1
+ # fix https://github.com/lxml/lxml/commit/57f49eed82068a20da3db8f1b18ae00c1bab8b12#L1L1139
+ selected_options = ele.xpath('.//option[@selected]')
+ v = [(o.get('value') or o.text or u'').strip() for o in selected_options]
return n, v
+
def _get_clickable(clickdata, form):
"""
Returns the clickable element specified in clickdata,