简体   繁体   English

如何使用Mechanize提交JavaScript表单?

[英]How to submit javascript form with Mechanize?

I have mechanize script that is done logging in. After log in. The page shows a redirect first before going into the main logged in page. 我已经完成了机械化脚本的登录。登录后。在进入登录主页面之前,该页面首先显示重定向。

Executing redirect() brings me back to the login page. 执行redirect()使我回到登录页面。 Why? 为什么?

Executing login() gives me this page w/c is right but still needs to continue to the main page. 执行login()给我这个页面,w / c是正确的,但仍然需要继续进入主页。

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title></head>
<body>
     <form name="form1" method="post" action="tmp.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZCOgyU+AdP30f85W4DdUIV6LnCqa" />
</div>

      <script type="text/javascript">
          top.location.href = document.location.href;
          document.forms["form1"].submit();
      </script>
          &nbsp;&nbsp;</form>
      </body>
</html>

I don't really know what to do as for I am new at this. 我真的不知道该怎么办,因为我是新来的。

How do I submit this kind of form using the already authenticated data provided from my first login? 如何使用首次登录时提供的已认证数据提交此类表格?

Also how to submit more POST data with the authenticated user? 另外,如何通过身份验证的用户提交更多POST数据?

My code so far: 到目前为止,我的代码:

import re
import mechanize

login_url = 'login.aspx'

def login(id, username, password):
    br = mechanize.Browser()
    br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
    br.open(login_url)
    br.select_form(nr=0)
    br.form.set_all_readonly(False)
    br["__EVENTTARGET"] = "TransactBtn"
    br["AccountID"] = id
    br["UserName"] = username
    br["Password"] = password   
    response = br.submit()
    return response.geturl()
    #after submitting this it goes to the redirect portal page then to the main page

def redirect(url): 
    #after login we submit the redirect portal to see the main page
    br = mechanize.Browser()
    br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
    br.open(url)
    br.select_form(nr=0)
    response = br.submit()
    return response.read() #to the main

def dostuff():
    #this will submit some data as POST with the authenticated user.

print redirect(login('myid', 'myusername', 'mypassword'))

I think you've got this problem because you're creating new instance of mechanise for any request. 我认为您遇到了这个问题,因为您正在为任何请求创建机械的新实例。 Mechanise is somewhat like a browser, with cookies storage and so on. Mechanise有点像浏览器,具有cookie存储等。 And re-creation of it's object is totally like clearing all data in browser. 重新创建它的对象就像清除浏览器中的所有数据。

So, you must share one single instance of Browser class among all your requests. 因此,您必须在所有请求中共享一个Browser类实例。

login function looks like doing what you need, try to print br._ua_handlers['_cookies'].cookiejar to ensure all cookies are set by login handler on server, and then use the same instance of Browser to pull the pages you need. login功能看起来像在做您需要的事情,尝试打印br._ua_handlers['_cookies'].cookiejar以确保服务器上的登录处理程序设置了所有cookie,然后使用Browser的同一实例提取所需的页面。

Best of all I think is to create a class and set Browser it's class variable. 我认为最重要的是创建一个类,并将Browser设置为class变量。

class MyWorker(object):
    def __init__(self):
        self._br = mechanize.Browser()
        self._br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

    def login(self):
        self._br.open(login_url)
        self._br.select_form(nr=0)
        self._br.form.set_all_readonly(False)
        self._br["__EVENTTARGET"] = "TransactBtn"
        self._br["AccountID"] = id
        self._br["UserName"] = username
        self._br["Password"] = password   
        self._br.submit()

I can be wrong, but looks like Javascript here doesn't really matters. 我可能是错的,但看起来这里的Javascript并不重要。

Mechanize does not support javascript. 机械化不支持javascript。 You should look at Selenium , which does pretty much the same thing as mechanize, but handles javascript. 您应该看一下Selenium ,它与机械化几乎一样,但是可以处理javascript。

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

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