无法访问HTML表单上的文本框

时间:2012-10-12 13:21:51

标签: python forms login input mechanize

我找不到登录文本框。我可以选择表单,但我无法找到输入文本。有人能帮助我吗?

我正在尝试使用http://www.safilonet.safilo.com/上的此脚本自动登录 感谢

以下是代码和错误消息:

def login(self):
url = self._baseUrl + '/webapp/commerce/safilo/jsp/logon.jsp?lng=E&cpy=10'
self._br.open(url, timeout=30.0)
self._br.select_form(name='logon') 
print 'entrou'
self._br.form['login'] = self._username
self._br.form['password'] = self._password
self._br.submit()

if self._verbose: print 'Login successful.\n'

self.last_logged_in = datetime.datetime.now()

错误讯息:

Traceback (most recent call last):
File "E:\billy\Projects\GlassInteg\trunk\src\run.py", line 90, in <module> go()
File "E:\billy\Projects\GlassInteg\trunk\src\run.py", line 39, in go ss = SafiloSupplier(updateBrands=True)
File "E:\billy\Projects\GlassInteg\trunk\src\GlassInteg\Supplier.py", line 283, in __init__
if login: self.login()
File "E:\billy\Projects\GlassInteg\trunk\src\GlassInteg\Supplier.py", line 307, in login
self._br.form['login'] = self._username
File "build\bdist.win32\egg\mechanize\_form.py", line 2780, in __setitem__
File "build\bdist.win32\egg\mechanize\_form.py", line 3101, in find_control
File "build\bdist.win32\egg\mechanize\_form.py", line 3185, in _find_control

mechanize._form.ControlNotFoundError:没有控件匹配名称'login'

1 个答案:

答案 0 :(得分:0)

根据W3C validator

,问题似乎是相关网页包含大量无效的HTML

如您所见,机械化无法看到“登录”字段:

>>> from mechanize import Browser
>>> br = Browser()
>>> br.open('http://www.safilonet.safilo.com/webapp/commerce/safilo/jsp/logon.jsp?lng=E&cpy=10')
<response_seek_wrapper at 0x1011cc638 whose wrapped object = <closeable_response at 0x1011cc488 whose fp = <socket._fileobject object at 0x1002bd650>>>
>>> br.select_form('logon')
>>> br._pairs()
[('merchant', '23888'), ('lng', 'E'), ('cpy', '10'), ('rightPageURL', 'http://www.safilonet.safilo.com/webapp/commerce/catalog/home.html'), ('password', '')]

如果您可以控制此页面,请尽快修复HTML。

但是,我认为你没有,所以这里有一些替代选择:

  • 使用selenium
  • 尝试使用直接POST网址登录(不确定机械化是否支持此功能)