File test.patch of Package python-MechanicalSoup
Index: mechanicalsoup-1.4.0/mechanicalsoup/stateful_browser.py
===================================================================
--- mechanicalsoup-1.4.0.orig/mechanicalsoup/stateful_browser.py
+++ mechanicalsoup-1.4.0/mechanicalsoup/stateful_browser.py
@@ -231,16 +231,23 @@ class StatefulBrowser(Browser):
raise LinkNotFoundError
form = selector
else:
- # nr is a 0-based index for consistency with mechanize
- found_forms = self.page.select(selector,
- limit=nr + 1)
- if len(found_forms) != nr + 1:
+ m = re.fullmatch(r"form\[\s*([a-zA-Z_-]+)\s*\$\s*=\s*['\"](.+?)['\"]\s*\]", selector.strip())
+ if m:
+ attr, suffix = m.groups()
+ found_forms = [
+ f for f in self.page.find_all("form")
+ if f.has_attr(attr) and f[attr].endswith(suffix)
+ ]
+ else:
+ found_forms = self.page.select(selector)
+
+ if len(found_forms) <= nr:
if self.__debug:
print('select_form failed for', selector)
self.launch_browser()
raise LinkNotFoundError()
- form = found_forms[-1]
+ form = found_forms[nr]
if form and form.has_attr('id'):
form_id = form["id"]