Python,机械化 - 按名称查找表单的问题

时间:2011-09-01 10:12:38

标签: python forms mechanize

import urllib
import cookielib
import mechanize

url = "http://hattrick.org/World/Transfers/"

cookie = cookielib.CookieJar()
browser = mechanize.Browser()

browser.set_cookiejar(cookie)
browser.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

browser.open(url)
browser.select_form(name="aspnetForm")
browser.form['ctl00$ctl00$CPContent$ucSubMenu$ucLogin$txtUserName'] = 'login8192'
browser.form['ctl00$ctl00$CPContent$ucSubMenu$ucLogin$txtPassword'] = '8192login'

response = browser.submit()

browser.select_form(name="aspnetForm")


#<select name="ctl00$ctl00$CPContent$CPMain$ddlSkill1" id="ctl00_ctl00_CPContent_CPMain_ddlSkill1" class="skillDropDown" style="width:120px;">

#control=browser.form.find_control("ctl00$ctl00$CPContent$CPMain$ddlSkill1")# <--- here is the problem

#<select name="ctl00$ctl00$CPContent$CPMain$ddlSkill1Min" id="ctl00_ctl00_CPContent_CPMain_ddlSkill1Min"
#onchange="primaryMinSelected('ctl00_ctl00_CPContent_CPMain_ddlSkill1Min', 'ctl00_ctl00_CPContent_CPMain_ddlSkill1Max');"
#onkeyup="primaryMinSelected('ctl00_ctl00_CPContent_CPMain_ddlSkill1Min', 'ctl00_ctl00_CPContent_CPMain_ddlSkill1Max');" style="width:120px;">

control=browser.form.find_control("ctl00$ctl00$CPContent$CPMain$ddlSkill1Min")
#this works fine

response=browser.submit()

问题是:

control=browser.form.find_control("ctl00$ctl00$CPContent$CPMain$ddlSkill1")

无法找到。

---------------------------
Error
---------------------------
ControlNotFoundError: no control matching name 'ctl00$ctl00$CPContent$CPMain$ddlSkill1'
---------------------------
OK   
---------------------------

1 个答案:

答案 0 :(得分:7)

您可以使用browser.forms()迭代器检查可用的表单。 要找出Python解释器中可用的表单,请执行以下操作:

>>> print [form for form in browser.forms()][0]