[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1070
  • Last Modified:

Calling a Javascript function in Python

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(){window.top.location='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!  ^_^
  • 2
  • 2
1 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'.
teltelAuthor Commented:
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!
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.
teltelAuthor Commented:

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now