Python Mechanize select form FormNotFoundError

强颜欢笑 提交于 2019-12-03 12:48:40

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)

a little more readable:

class Element_by_id:
    def __init__(self, id_text):
        self.id_text = id_text
    def __call__(self, f, *args, **kwargs):
        return 'id' in f.attrs and f.attrs['id'] ==self.id_text

then:

b.select_form(predicate=Element_by_id("login_form"))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!