How to click a javascripted' radio button and fetch next page using Python (urllib2)? [WebScrape, selenium]
I am struggling with this. I have a fully tested python script. I have to make a small change wherein I have to first click on a radio button which in turn automatically executes a javascript function forwarding the page to a search form.
My working platform : Linux
Language : Python
Radio button code :
<input type="radio" language="javascript" onclick="javascript:setTimeout('__doPostBack(\'_ctl0$Main$SearchRadio\',\'\')', 0)" value="SearchRadio" name="_ctl0:Main:SearchRadio" id="_ctl0_Main_SearchRadio">
Javascript code on the page :
<script type=开发者_Python百科"text/javascript">
<!--
var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
// -->
</script>
This is what selenium does. Not sure if it helps at all.
def test_untitled(self):
sel = self.selenium
sel.open("/Search/results.aspx")
sel.click("_ctl0_Main_SearchRadio")
sel.wait_for_page_to_load("30000")
sel.type("-InputTextField", "ThinkCode")
sel.click("_SearchMainUser")
sel.wait_for_page_to_load("30000")
try: self.failUnless(sel.is_text_present("ThinkCode"))
except AssertionError, e: self.verificationErrors.append(str(e))
All I am trying to do is go to the resulting page that we are redirected to once we click the radio button. I read about javascript horrors in python web-scraping using urllib2. Hope a kind pythonista will help me soon :)
Instead of trying to automate clicking the JavaScript button, which is difficult in Python, try monitoring what request is sent and then replicate this. There are various firefox extensions that will help you do this, such as TamperData, Firebug, and LiveHttp.
精彩评论