简体   繁体   English

如何使用Python打开.html文件并单击Submit按钮

[英]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文件,其中使用“提交”按钮发送值,如下所示:

<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. 我想要一个.html并进行维护,因此必须打开并单击它。 A cgi/python example did come into my notice but I would go for it only if there is no other alternative. 我确实注意到了一个cgi / python示例,但是只有在没有其他选择的情况下,我才会去使用它。

How can I use python to: 如何使用python执行以下操作:

  1. Open the .html file and 打开.html文件,然后
  2. Press the "Send Data" button 按下“发送数据”按钮
  3. Read any response given (assuming the response maybe displayed within a HTML page or a dialog box) 阅读给出的任何响应(假设响应可能显示在HTML页面或对话框中)

Python code for sending Data 用于发送数据的Python代码

`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 用于将值从python作为字典传递到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. 使用flask来承载HTML页面,并使用POST请求与python脚本之间来回发送数据。

This link should help you more : https://www.tutorialspoint.com/flask/index.htm 该链接应该为您提供更多帮助: https : //www.tutorialspoint.com/flask/index.htm

"Clicking" a button is nothing more than a POST request with the form data in the body. “单击”按钮只不过是在正文中包含表单数据的POST请求。

If you need something generic, you would have to parse the HTML, find what data the host accepts and POST it. 如果需要通用的东西,则必须解析HTML,找到主机接受的数据并将其发布。

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 但是,如果您仅需要此示例,即意味着您已经知道服务器接受的数据,则可以不用考虑HTML,而可以使用诸如请求之类的方式发布数据

I believe this is exactly what you are looking for .Its a simple python server with the baseHttpHandler of Python. 我相信这正是您要寻找的东西。它是带有Python的baseHttpHandler的简单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. 您可以通过传递您选择的适当端口来运行方法来运行代码,否则将使用默认的80。 To test this or to do a get or post , you could run a curl as follows: 要对此进行测试或进行get或post,可以按如下所示运行curl:

Send a GET request:: curl http://localhost 发送GET请求:: curl http:// localhost

Send a HEAD request:: curl -I http://localhost 发送HEAD请求:: curl -I http:// localhost

Send a POST request:: curl -d "foo=bar&bin=baz" http://localhost 发送POST请求:: 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. 您还可以创建一个单独的index.html文件,并使用python中的编解码器进行读取。 Since the input would be string , it could be tampered with , eventually displaying the desired page. 由于输入将是string,因此可能会对其进行篡改,最终显示所需的页面。

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

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