Python Mechanize select form FormNotFoundError

后端 未结 2 1020
时光说笑
时光说笑 2021-02-14 10:03

I want to select a form with mechanize. This is my code:

br = mechanize.Browser()
self.br.open(url)
br.select_form(name=\"login_form\")

The for

2条回答
  •  一个人的身影
    2021-02-14 10:34

    The problem is that your form does not have a name, only an id, and it is login_form. You can use a predicate:

    br.select_form(predicate=lambda f: f.attrs.get('id', None) == 'login_form')
    

    (where you se if f.attrs has the key id and, if so, the id value is equal to login_form). Alternatively, you can pass the number of the form in the page, if you know if it is the first one, the second one etc. For example, the line below selects the first form:

    br.select_form(nr=0)
    

提交回复
热议问题