ClientForm.AmbiguityError:多个控件匹配名称

时间:2016-02-05 14:07:33

标签: python mechanize

我是python中的新编程,最近尝试使用mechanize登录私人网站;我读过类似的问题: How to bypass Mechanize "AmbiguityError" in Pythonpython mechanize handle two parameters with same name,但是这些中没有一个可以提供帮助,并且没有针对第二个链接的解决方案的反馈。据我所知,使用br.select_form(nr = 0)应该足以选择第一个表格但是我仍然卡住;我尝试过更改br.select_form(表单名称)和br.form.find_control()以及AttributeError:' NoneType'对象没有属性' find_control&#39 ;;所有选项都没有成功。 可以找到下面的代码和表单列表。支持表示赞赏。感谢

这是使用的代码:

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()

这些是形式:

<HiddenControl(smauthreason=0) (readonly)>
<HiddenControl(clientfp=) (readonly)>
<HiddenControl(smtryno=0) (readonly)>
<TextControl(username=)>
<PasswordControl(password=)>
<TextControl(username=)>
<PasswordControl(password=)>
<TextControl(username=)>
<PasswordControl(password=)>
<IgnoreControl(submitFrm=<None>)>
<HiddenControl(SMAGENTNAME= "deleted by me") (readonly)>
<HiddenControl(POSTPRESERVATIONDATA=) (readonly)>>

结果如下:

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'

1 个答案:

答案 0 :(得分:1)

看起来提供的代码只有一个表单,多个用户名,密码字段是Ambiguous错误的来源。你可以使用index参数来选择表单,如下所示:

br.select_form(nr=0)
userone = br.find_control(name="username", nr=0)
userone.value = "Myusername"
pwone = br.find_control(name="password", nr=0)
pwone.value = "Mypassword"
br.submit()