简体   繁体   English

如何单击使用javascript的单选按钮并使用Python(urllib2)获取下一页? [WebScrape,硒]

[英]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. 我有一个经过全面测试的python脚本。 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. 我必须做一个小的更改,其中我必须首先单击一个单选按钮,该单选按钮又自动执行将页面转发到搜索表单的javascript函数。

My working platform : Linux 我的工作平台:Linux

Language : Python 语言: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 : 页面上的JavaScript代码:

<script type="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. 我了解了使用urllib2在python网络抓取中的javascript恐怖。 Hope a kind pythonista will help me soon :) 希望一个好心的pythonista会尽快帮助我:)

Instead of trying to automate clicking the JavaScript button, which is difficult in Python, try monitoring what request is sent and then replicate this. 而不是尝试自动单击JavaScript按钮(这在Python中是很难的),而是尝试监视发送了什么请求,然后复制该请求。 There are various firefox extensions that will help you do this, such as TamperData , Firebug , and LiveHttp . 有各种各样的firefox扩展可以帮助您做到这一点,例如TamperDataFirebugLiveHttp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM