【问题标题】:ClientForm.AmbiguityError: more than one control matching nameClientForm.AmbiguityError:多个控件匹配名称
【发布时间】:2016-02-05 14:07:32
【问题描述】:

我是 python 的新程序员,最近尝试使用 mechanize 登录私人网站;我读过类似的问题,例如: How to bypass Mechanize "AmbiguityError" in Pythonpython mechanize handle two parameters with same name,但是这些都没有帮助,并且没有关于第二个链接的解决方案的反馈。据我所知,使用 br.select_form(nr=0) 应该足以选择第一个表单,但是我仍然卡住了;我也试过用 AttributeError 改变 br.select_form(name of form) 和 br.form.find_control(): 'NoneType' object has no attribute 'find_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()

这些是表格:

<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'

【问题讨论】:

    标签: python mechanize


    【解决方案1】:

    看起来提供的代码中只有一个表单和多个用户名、密码字段,这是不明确错误的来源。您可以像选择表单一样使用 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-20
      • 2014-03-17
      • 1970-01-01
      • 1970-01-01
      • 2020-12-08
      • 1970-01-01
      • 1970-01-01
      • 2020-10-07
      相关资源
      最近更新 更多