Python, mechanize - problem with finding form by name

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

The problem is that:

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

could not be found.

 --------------------------- Error --------------------------- ControlNotFoundError: no control matching name 'ctl00$ctl00$CPContent$CPMain$ddlSkill1' --------------------------- OK --------------------------- 
+4
source share
1 answer

You can check which forms are available using the browser.forms () iterator. To find out what forms are available in the Python interpreter, simply do:

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

All Articles