Question

I'm new to mechanize and also not the most advanced python user, but I want to automize a task in which I want to give a webpage an input. Problem is now, that the "submit" Button has no control name assigned to it. So I researched a bit and found a way to set a value to the form in question. But to do this I have to access the specific form I want to assign a value to. So my code looks like this:

forms = [f for f in br.forms()]
print forms[0].controls[0].name

I just thought I could access the form by write forms[x] and then something like:

forms[x].set_value("VALUE", 
                       nr=5)

The error I get is:

    forms[54].set_value("VALUE",nr=100)
IndexError: list index out of range

This might be a bit of a stupid question and possibly arises from the fact that I don't really understand the functions I'm using but since there is no real documentary I would really appreciate a sentence of help here.

PS: I can print all the forms using

for f in br.forms():
    print f

with output:

 <CheckboxControl(lookup=[yes])>
  <TextControl(fld=NoName)>
  <TextControl(pixemail=)>
  <IgnoreControl(<None>=<None>)>
  <TextControl(ra=00 00 00.0)>
  <TextControl(dec=00 00 00.0)>
  <SelectControl(equinox=[*J2000.0, B1950.0])>
  <TextControl(offra=0.0)>
  <TextControl(offdec=0.0)>
  <TextControl(epoch=2000.0)>
  <SubmitControl(<None>= Retrieve Data ) (readonly)>
  <RadioControl(cextract=[*rect, circle])>
  <TextControl(rawid=10.0)>
  <TextControl(decwid=10.0)>
  <SelectControl(wunits=[Degrees, *Minutes, Seconds])>
  <TextControl(cirrad=10.0)>
  <SelectControl(cat=[UCAC 2, UCAC 3, NOMAD, *USNO B1.0, USNO A2.0, ACT])>
  <SelectControl(surims=[None, *All Surveys, POSS-I (103aO, 103aE), POSS-II (IIIaJ, IIIaF, IV-N), SOUTH, AAO-R, POSS-IO, POSS-IE, POSS-IIJ, POSS-IIF, POSS-IIN, SRC-J, SERC-EJ, ESO-R, SERC-ER])>
  <CheckboxControl(getcat=[*yes])>
  <CheckboxControl(getfin=[*yes])>
  <CheckboxControl(pixflg=[yes])>
  <CheckboxControl(colbits=[All, *cb_id, *cb_altid, *cb_ra, *cb_sigra, cb_mep, *cb_mura, cb_muprob, *cb_smura, cb_sfitra, *cb_fitpts, cb_err, *cb_flg, *cb_mag, cb_smag, *cb_mflg, *cb_fldid, *cb_sg, cb_xres, cb_pltidx, *cb_xi, *cb_dstctr, *cb_gall])>
  <RadioControl(skey=[*ra, dec, sigra, sigdec, mep, mura, mudec, muprob, smura, smudec, sfitra, sfitdec, fitpts, err, flg, mag, smag, mflg, fldid, sg, xres, yres, pltidx, clr, sigpos, mutot, sigmu, xi, eta, dstctr, gall, galb])>
  <SelectControl(slf=[*hh/dd mm ss, hh/dd:mm:ss, hh.hhh/dd.ddd, ddd.ddd/dd.ddd])>
  <TextControl(minnpts=0)>
  <TextControl(maxnpts=10)>
  <SelectControl(clr=[B1, R1, B2, *R2, I2, B, V, R, J, H, K])>
  <TextControl(bri=0)>
  <TextControl(fai=100)>
  <SelectControl(clr0m1A=[B1, R1, *B2, R2, I2, B, V, R, J, H, K])>
  <SelectControl(clr0m1B=[B1, R1, B2, *R2, I2, B, V, R, J, H, K])>
  <TextControl(bmrmin=-100)>
  <TextControl(bmrmax=100)>
  <TextControl(minposnerr=0.0)>
  <TextControl(maxposnerr=10000.0)>
  <TextControl(mumin=0.0)>
  <TextControl(mumax=10000.0)>
  <TextControl(minmuerr=0.0)>
  <TextControl(maxmuerr=10000.0)>
  <TextControl(minsep=0.0)>
  <HiddenControl(minmagerr=0.0) (readonly)>
  <HiddenControl(maxmagerr=1.0) (readonly)>
  <SelectControl(opstars=[Yes, *No])>
  <SelectControl(whorbl=[Light Stars/Dark Sky, *Dark Stars/Light Sky])>
  <SelectControl(pixgraph=[Progressive JPEG, *JPEG, GIF, PDF, Large JPEG (1 Survey Only), Large GIF (1 Survey Only), PS (1 Survey Only)])>
  <SelectControl(pixfits=[Yes, *No])>
  <SelectControl(ori=[NE - North Up, East Right, *NW - North Up, East Left, SE - North Down, East Right, SW - North Down, East Left, EN - East Up, North Right, ES - East Up, North Left, WN - East Down, North Right, WS - East Down, North Left])>
  <SelectControl(tck=[N and E marks, *Tick Marks, Grid Lines])>
  <SelectControl(starlbl=[Yes, *No])>
  <SelectControl(cmrk=[*None, 5.0 sec Box, 10.0 sec Box, 30.0 sec Box, 1.0 min Box, 2.0 min Box, 5.0 min Box, 10.0 min Box, 5.0 sec Circle, 10.0 sec Circle, 30.0 sec Circle, 1.0 min Circle, 2.0 min Circle, 5.0 min Circle, 10.0 min Circle])>
  <TextControl(aobj=none)>
  <SelectControl(pcl=[*P - Points, L - Points + Labels, C - Connected Points, A - Connected Points + Labels])>
  <TextareaControl(atbl=  )>
  <IgnoreControl(<None>=<None>)>
  <SubmitControl(<None>= Retrieve Data ) (readonly)>
  <SelectControl(gzf=[*Yes, No])>
  <SelectControl(cftype=[*ASCII, XML/VO])>>

The one I want to get my hands on is the <SubmitControl(<None>=Retrieve Data ) (readonly)>, which is the third one counted from bottom to top.

Was it helpful?

Solution

Try this:

import mechanize

br = mechanize.Browser()

# Insert the desired URL here
br.open('http://www.nofs.navy.mil/data/fchpix/cfch.html#fchmenu')
br.select_form(nr=0)

br["ra"] = "input 1"
br["dec"] = "input 2"
br["pixfits"] = ["Yes"]

br.find_control("pixflg").items[0].selected=True

response = br.submit()

OTHER TIPS

I'm quite certain there is a better way to do this - but I'm not at all familiar with Mechanize. You could do something like:

submit_values = filter(lambda x: 'SubmitControl' in str(x), br.forms())
if submit_values:
    print(submit_values[0])

If there are more than one, obviously you'll get more than one. This is probably the weirdest way to accomplish whatever it is you're trying to do. Also, assuming this form is fairly static, you could replace the use of Mechanize with requests. Then it would look something like this:

import requests

r = requests.post("http://form/action/url/goes/here", data={"lookup": "yes",
                                                            # all the other elements
                                                            })
print(r.status_code)
print(r.text)
# Do something else with r.text, e.g. scrape values with beautifulsoup or something

try this. You can search for a control without a name by the type of control. Just going from memory it would be:

br.form.find_control(type='submit', nr=1)

I think thats the right syntax.. I'll double check and be sure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top