Calling a Javascript function in Python

Posted on 2005-05-12
Last Modified: 2013-12-16
Hi All,

I am writing a python script to open up some javascript webpages in a device.  I need to click a button to reset the device.  So I am basically asking: is there a way to "click" a button in Python, or call the javascript onReset() function?  I have tried the ClientForm module but it cannot parse out the button that I want correctly.  Below is the source code of the webpage just for info:

<html><head><link rel=stylesheet href=style.css type=text/css><title>Reset</title></head><body><form name=resetform><table border=0><tr><td class=header><strong>Reset</strong></td></tr><tr><td class=small><strong>Warning! Resetting the system will terminate all network connections and reset your browser connection.</strong></td></tr><tr><td><hr></td></tr><tr><td><input type=radio checked name=RESET value=NORMAL checked> Reset and execute Main Application</td></tr><tr><td><input type=radio name=RESET value=STAGE1> Reset and execute Downloader Application</td></tr><tr><td><hr></td></tr><tr><td><input type=button class=button value=" Reset " onclick=onReset()></td></tr></table></form><script language="JavaScript"><!--

function onReset(){for(i=0;i<document.resetform.RESET.length;i++){if(document.resetform.RESET[i].checked) document.hform.RESET.value=document.resetform.RESET[i].value;}document.hform.submit();setTimeout("Wait()",500);return true;}function Wait(){document.write("<html><body bgcolor=#FFFFFF><center>"+"Please wait while the BVP8762 is rebooted..."+"</body><script language='JavaScript'>"+"function Retry(){'index.htm';setTimeout('Retry()',500);}"+"</script></html>");document.close();setTimeout("Retry()",2000);return true;}//--></script><form name=hform method=POST><input type=hidden name=RESET value=NORMAL></form></body></html>

I don't know much javascript and am new to Python.  Thanks for any help!  ^_^
Question by:teltel
    LVL 15

    Accepted Solution

    from urllib import urlopen, urlencode
    if main_app:
      reset = 'NORMAL' # main app
      reset = 'STAGE1' # downloader app
    urlopen(device_url, urlencode({'RESET':reset})).read()

    Depending on user input you set 'main_app' to True if you want to reset main application.
    Also set device_url to 'http://something'.
    LVL 1

    Author Comment

    Thanks mish33, it works perfectly.

    Would you mind explaining the last line a bit more please?  I am still a little confused as to how/why it would work.  Many thanks again!
    LVL 15

    Expert Comment

    Basicly I translated from Javascript to Python.

    HTML code in the start is the first form to allow user select the type of reset.
    Then onClick call Javascript which substitutes user selected value from the the first form to the second form and sends it.
    So inside urlencode you see data from the second form and urlopen().read() sends it.

    Don't hesistate to ask for further detail clarification.
    LVL 1

    Author Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Suggested Solutions

    Title # Comments Views Activity
    looping 11 18
    Downgrade Python 3.5 to 3.3 29 180
    Trying to run a Python File 11 48
    how to split multiple lines delimiter : 8 12
    A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
    The purpose of this article is to demonstrate how we can use conditional statements using Python.
    The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…
    The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now