我是python编程新手,最近尝试用mechanize登录私有网站;我读到类似的问题,比如: How to bypass Mechanize "AmbiguityError" in Python,python mechanize handle two parameters with same name,但是这些都没有帮助,并且没有关于第二个链接的解决方案的反馈。据我所知,使用br.选择表(nr=0)应该足够选择第一个表单,但是我还是被卡住了;我也尝试过改变br.选择表(表格名称)和br.form.find_控件()with AttributeError:“NoneType”对象没有属性“find\u control”;所有选项都没有成功。 下面的代码和表单列表可以找到。感谢支持。谢谢
这是使用的代码:
br = mechanize.Browser()
br.set_handle_robots(False)
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.select_form(nr=0)
br.form["username"]= 'Myusername'
br.form["password"]= 'Mypassword'
br.submit()
表格如下:
^{pr2}$结果是:
Traceback (most recent call last):
File "C:/Documents/JMARTINEZ/My Various/PythonPrograms/Mechanize.py",line 77, in <module> br.form["username"]= username
File "C:\Python27\lib\site-packages\ClientForm.py", line 2895, in __setitem__control = self.find_control(name)
File "C:\Python27\lib\site-packages\ClientForm.py", line 3222, in find_control return self._find_control(name, type, kind, id, label, predicate, nr)
File "C:\Python27\lib\site-packages\ClientForm.py", line 3304, in _find_control raise AmbiguityError("more than one control matching "+description)ClientForm.AmbiguityError: more than one control matching name 'username'
从提供的代码中似乎只有一个表单和多个username、passwords字段,这就是产生歧义错误的原因。您可以像选择表单一样使用index参数,如下所示:
相关问题 更多 >
编程相关推荐