简体   繁体   中英

How to open a .html file and click submit button using python

I have a .html file where I am sending a value using the submit button as follows:

<HTML>
<HEAD>
<TITLE>XYZ Ltd.</TITLE>
</HEAD>
<BODY>
<FORM ACTION="http://192.168.2.2/cgi-bin/http_recv.cgi" METHOD="POST">
<TEXTAREA NAME="DATA_SEND" COLS='160' ROWS='40' WRAP='none'>

</TEXTAREA>
<INPUT TYPE="SUBMIT" VALUE="Send Data">
</FORM>
</BODY>
</HTML>

I did go through selenium and from my understanding it doesn't suit me. I would like to have a .html as above and maintain it, so it has to be opened and clicked. A cgi/python example did come into my notice but I would go for it only if there is no other alternative.

How can I use python to:

  1. Open the .html file and
  2. Press the "Send Data" button
  3. Read any response given (assuming the response maybe displayed within a HTML page or a dialog box)

Python code for sending Data

`def hello():
    Dict={'Title': 'This is title','Subtitle':'subtitle'}
    return render_template('hello.html',Dict=Dict)`

Code for writing values which is passed from python as dictionary into HTML

`<form accept-charset="utf-8" class="simform" method="POST" 
    enctype=multipart/form-data>
    Title <input type="text" name="Title" value="{{ Dict.get('Title') 
                 }}" maxlength="36">                                   
    SubTitle <input type="text" name="SubTitle" value="{{ 
    Dict.get('SubTitle') }}" maxlength="70">
    <button type="submit" class="save btn btn-default">Submit</button>
</form>`

Use flask to host your HTML Page and use a POST request to send data to and from your python script.

This link should help you more : https://www.tutorialspoint.com/flask/index.htm

"Clicking" a button is nothing more than a POST request with the form data in the body.

If you need something generic, you would have to parse the HTML, find what data the host accepts and POST it.

But if you just need this for this example, meaning, you already know the data the server accepts, you can just forget about the HTML and just use something like requests to post the data

I believe this is exactly what you are looking for .Its a simple python server with the baseHttpHandler of Python.

class S(BaseHTTPRequestHandler):
    def _set_headers(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        self._set_headers()
        self.wfile.write("<html><body><h1>hi!</h1></body></html>")

    def do_HEAD(self):
        self._set_headers()

    def do_POST(self):
        # Doesn't do anything with posted data
        self._set_headers()
        self.wfile.write("<html><body><h1>POST!</h1></body></html>")

def run(server_class=HTTPServer, handler_class=S, port=80):
    server_address = ('', port)
    httpd = server_class(server_address, handler_class)
    print 'Starting httpd...'
    httpd.serve_forever()

You can run the code by passing an appropriate port of your choice to run method or the default 80 will be used. To test this or to do a get or post , you could run a curl as follows:

Send a GET request:: curl http://localhost

Send a HEAD request:: curl -I http://localhost

Send a POST request:: curl -d "foo=bar&bin=baz" http://localhost

You could also create a seperate file of index.html and read using the codecs in python. Since the input would be string , it could be tampered with , eventually displaying the desired page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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