简体   繁体   中英

Use Python to call a JavaScript function that is on a webpage

I created a webpage that has one JavaScript function called foo(z) . For the sake of this question, let's assume that all foo(z) does is call console.log(z) .

Is it possible to run a Python script that can trigger the JavaScript foo(z) function that exists on the webpage?

In other words, I load up the webpage in my browser that contains foo(z) . Then I execute the Python script on my local machine and it reaches into the browser and calls the JavaScript foo(z) function and you thus see z output to the browser console (because all foo(z) does is call console.log(z) ).

It seems like there is a lot of info on executing JavaScript with Python, but I don't think any of those resources deal with executing a JavaScript function that is inside a webpage.

Edit: I built a game on a webpage that humans can play. Now I want to create a Python bot that can call the JavaScript functions on the webpage so that it too can play the game.

You cannot directly execute the javascript from python.

If you are running a local server, you can communicate with the page using a websocket and make it execute the JS function when the python page emits a signal to do so. The Flask framework can be useful.

You can also see this question : How do I call a Javascript function from Python? .

Hi so one possible solution would be to use ajax with flask to comunicate between javascript and python. You would run a server with flask and then open the website in a browser. This way you could run javascript functions when the website is created via pythoncode or with a button how it is done in this example.

in getJSON the function argument receives the result of the python function. here you could run js code depending on the output and run your function foo(z) as an if condition

also with flask you determine which js code you want to show to your client.

HTML code:



<html>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>


<script>
    function pycall() {
      $.getJSON('/pycall', {content: "content from js"},function(data) {
          //run here your function on if(data.result==0) for example
          alert(data.result);
      });
    }
</script>


<button type="button" onclick="pycall()">click me</button>
 

</html>

Python Code:

from flask import Flask, jsonify, render_template, request

app = Flask(__name__)


def load_file(file_name):
    data = None
    with open(file_name, 'r') as file:
        data = file.read()
    return data

@app.route('/pycall')
def pycall():
    content = request.args.get('content', 0, type=str)
    
    print("call_received",content)
    return jsonify(result="data from python")

@app.route('/')
def index():
    return load_file("basic.html")



import webbrowser
print("opening localhost")
url = "http://127.0.0.1:5000/"
webbrowser.open(url)
app.run()

output in python:

call_received content from js

alert in browser:

data from python

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