问题
I'm using mechanize on python 2.7.3. I'm looking for help on hitting the submit button on NIH's gene website.
The button is an actual button element surrounded by divs and not inside any form field.
<button id="search" type="submit" class="button_search nowrap" cmd="go">Search</button>
So far in examples and what I've seen online, submit buttons usually have a name or a label which mechanize can select for, or are in a form so I can do br.submit().
How would I hit submit on this one?
Thanks
回答1:
The button does not submit the for directly, but instead triggers a JavascriptEvent, which will eventually load the search page. This explains why the button works in your browser even though it is not inside the form.
mechanize
cannot run JS code, so you will not be able to "click" on the button directly using mechanize
.
Is there a reason you cannot just open the target URL? These seem pretty straightforward, even on the advanced search form, for example:
term = '("31"[Chromosome]) AND "000"[Disease/Phenotype]'
q = urllib.urlencode({'term': term})
response = mechanize.urlopen('http://www.ncbi.nlm.nih.gov/gene?%s' % q)
If this does not work for you, you'll have to fall back on a solution which does support JavaScript, see https://stackoverflow.com/questions/606550/watir-vs-selenium-vs-sahi for pointers.
来源:https://stackoverflow.com/questions/16177389/python-mechanize-submit-button-on-nih-website