python - HTML form has one javascript generated radio button. Is there a way around this using mechanize? -
this form, whatever reason, uses javascript's document.write
draw radio buttons on page, , can't figure out how circumvent it.
after sleuthing around here , on google, seems can manually recreate form field mechanize, , submit usual.
so, i've set code follows:
br = mechanize.browser(factory=mechanize.robustfactory()) response = br.open(url) br.select_form(nr=0) br.form.set_all_readonly(false) br.form.new_control('radio', 'datasource', {'value':'file', 'checked':'true'}) br.form.add_file(open('weather_info.csv'), 'text/csv', 'weather_info.csv', name='filename') br.form.fixup() response = br.submit()
now, if print form, appear 1 of fields @ bottom.
<hiddencontrol(cgiref=/calludt.cgi/ddfile1)> <hiddencontrol(use=model)> <hiddencontrol(model=cm)> <hiddencontrol(crop=apples)> <hiddencontrol(method=ss)> <hiddencontrol(units=e)> <hiddencontrol(lowthreshold=50)> <hiddencontrol(upthreshold=88)> <hiddencontrol(cutoff=h)> <selectcontrol(county=[])> <checkboxcontrol(active=[*y])> <selectcontrol(frommonth=[1, 2, *3, 4, 5, 6, 7, 8, 9, 10, 11, 12])> <selectcontrol(fromday=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, *15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])> <selectcontrol(fromyear=[2014, *2013, 2012, 2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001, 2000, 1999, 1998, 1997, 1996, 1995, 1994, 1993, 1992, 1991, 1990, 1989, 1988, 1987, 1986, 1985, 1984, 1983, 1982, 1981, 1980, 1979, 1978, 1977, 1976, 1975, 1974, 1973, 1972, 1971, 1970, 1969, 1968, 1967, 1966, 1965, 1964, 1963, 1962, 1961, 1960, 1959, 1958, 1957, 1956, 1955, 1954, 1953, 1952, 1951])> <selectcontrol(thrumonth=[1, 2, 3, 4, *5, 6, 7, 8, 9, 10, 11, 12])> <selectcontrol(thruday=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, *12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])> <selectcontrol(thruyear=[2014, *2013, 2012, 2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001, 2000, 1999, 1998, 1997, 1996, 1995, 1994, 1993, 1992, 1991, 1990, 1989, 1988, 1987, 1986, 1985, 1984, 1983, 1982, 1981, 1980, 1979, 1978, 1977, 1976, 1975, 1974, 1973, 1972, 1971, 1970, 1969, 1968, 1967, 1966, 1965, 1964, 1963, 1962, 1961, 1960, 1959, 1958, 1957, 1956, 1955, 1954, 1953, 1952, 1951])> <filecontrol(filename=weather_scrape.csv)> <ignorecontrol(submit=<none>)> <radiocontrol(datasource=[*file])>>
however, upon running code , submitting form nothing happens; stays on same page started on.
are there ways around using python?
edit:
i realized submit button javascript; has onclick
event tied so.. may out of luck far python solution goes..
<input type="button" name="submit" value="continue" onclick="setddinfo()">
i tried adding button manually form shown here.
br.form.new_control('submit', 'button', {})
but still no luck.
submit
's onclick
method calls following function:
function setddinfo(){ var ddstuff = new array(21); var checksok = true; // alert("setddinfo ddparms: " + ddparms); (var = 0; < 21; i++){ ddstuff[i] = ddparms[i]; } // section model document.ddcompute.use.value = "model"; ddstuff[4] = "model" with(document.ddcompute) { ddstuff[5] = "e"; // english units ddstuff[6] = ""; // lower threshold (null) ddstuff[7] = ""; // upper threshold (null) ddstuff[8] = "ss"; // dd method: (default) ddstuff[9] = "h"; // cutoff: (default) ddstuff[10] = "cm"; // organism model code: cm, now, etc. if (county.selectedindex < 0) { ddstuff[11] = ""; } else { ddstuff[11] = county.options[county.selectedindex].value; // county } if (datasource[0].checked == "1") { ddstuff[13] = "station"; // data database ddstuff[14] = ""; } else { if (datasource[1].checked == "1") { ddstuff[13] = "file"; // data user file ddstuff[14] = filename.value; } else { ddstuff[13] = "entry"; // data user entry ddstuff[14] = ""; } } } // model error checks. return false abort submit. if (ddstuff[11] == "" && ddstuff[13] == "station") { alert ("error: must select county."); checksok = false; return checksok } if (ddstuff[10] == "") { alert ("error: must select model."); checksok = false; return checksok } if (ddstuff[13] == "file" && ddstuff[14] == "") { alert ("error: must specify filename."); checksok = false; return checksok } // wx source error checks. return false abort submit. if (ddstuff[11] == "" && ddstuff[13] == "station") { alert ("error: must select county."); checksok = false; return checksok } if (ddstuff[13] == "file" && ddstuff[14] == "") { alert ("error: must specify filename."); checksok = false; return checksok } ddstuff[14] = ""; // don't bother saving filename, since not displayed , must re-specified. var dditems = ddstuff.join(","); setcookie ('ddinfo', dditems, exp); // alert ("ddstuff[13]: "+ddstuff[13]); if (ddstuff[13] == "station") { document.ddcompute.action = "/calludt.cgi/ddstationlist"; document.ddcompute.method = "get"; document.ddcompute.encoding = "application/x-www-form-urlencoded"; } else { if (ddstuff[13] == "file") { document.ddcompute.action = "/weather/textupload.cgi"; document.ddcompute.method = "post"; document.ddcompute.encoding = "multipart/form-data"; } else { document.ddcompute.action = "/calludt.cgi/ddentry1"; document.ddcompute.method = "get"; document.ddcompute.encoding = "application/x-www-form-urlencoded"; } } // alert ("document.ddcompute.action = " + document.ddcompute.action); document.ddcompute.submit() return checksok } // ................................................
i'm not fluent in javascript, first half of function seems dealing section of form i'm not interested in -- when radio button in position one
. second bit tough me follow, it's making post /weather/textupload.cgi
i'll have check out traffic tomorrow. maybe can recreate needs via urllib2
or something?
Comments
Post a Comment