Python open browser and run javascript function - Stack Overflow

I have a question about python and javascript. I like to create a file who minicate with javascript web

I have a question about python and javascript. I like to create a file who minicate with javascript webpage.

First i will create a Python file who will open a local webpage. This example localhost/test.html This page is a themplate page. The code is

<html><head><title>An example application</title></head>
<script>
function myFunction()
{
alert("Hello! I am an alert box!");
}
</script>
    <body>
    <h1 class="content">This is my sample application</h1>
    Put the content here...
    <hr>
    <a href="/exit">Quit</a>
    </body></html>

Whene everything is loaded and displayed the python file will activate the javascript function "myFunction()"

How do I do that? I am running Debian and have installed Python and Chromium.

I have a question about python and javascript. I like to create a file who minicate with javascript webpage.

First i will create a Python file who will open a local webpage. This example localhost/test.html This page is a themplate page. The code is

<html><head><title>An example application</title></head>
<script>
function myFunction()
{
alert("Hello! I am an alert box!");
}
</script>
    <body>
    <h1 class="content">This is my sample application</h1>
    Put the content here...
    <hr>
    <a href="/exit">Quit</a>
    </body></html>

Whene everything is loaded and displayed the python file will activate the javascript function "myFunction()"

How do I do that? I am running Debian and have installed Python and Chromium.

Share Improve this question edited Oct 9, 2012 at 10:45 Quentin 945k132 gold badges1.3k silver badges1.4k bronze badges asked Oct 9, 2012 at 10:42 Wouter van ReevenWouter van Reeven 5453 gold badges8 silver badges17 bronze badges 3
  • I deleted the webbrowser-control tag, ActiveX is not a realistic outside of Windows. – Quentin Commented Oct 9, 2012 at 10:46
  • can you give more background on this question? Why do you need browseer and not V8 binding? – Marat Commented Oct 9, 2012 at 13:55
  • Allright: I have a raspberry pi with a NFc reader connection. I have a browser. Chromium in this case. The pythonscript municate with the NFc reader. I this case it use the python-pyscard library. When i use my NFc card the python script need to access the browser. It will execute a javascipt what i need to use for this webpage – Wouter van Reeven Commented Oct 9, 2012 at 14:35
Add a ment  | 

3 Answers 3

Reset to default 5

You need a library to allow interaction between the browser and the program.

Options include Selenium and Ghost.

Have you looked at the webbrowser module ? It can be used to display your webpage. It can not call your js function, but perhaps that one can be triggered from the ready() event in the html?

You could open the website from python using webbroser module with an url parameter containing your javascript code.

in your example

import webbrowser
webbrowser.open('http://localhost/test.html' + '?script=myFunction();')

Then you can read the url paramater in your javascript and execute it via eval

<script>
    window.onload = function () {
        var url = new URL(window.location.href);
        var script = url.searchParams.get("script");
        console.log(script);
        eval(script)
    }    
</script>

Please be aware, that this is only suitable for testing or developing purposes and must not be used in production since using eval like this is dangerous.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744061969a4551943.html

相关推荐

  • Python open browser and run javascript function - Stack Overflow

    I have a question about python and javascript. I like to create a file who minicate with javascript web

    9天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信