PiotrDabkowski / Js2Py

JavaScript to Python Translator & JavaScript interpreter written in 100% pure Python🚀 Try it online:
http://piter.io/projects/js2py
MIT License
2.47k stars 261 forks source link

Window.location.href is not working when using within Javascript file. #249

Open rcnabendu opened 3 years ago

rcnabendu commented 3 years ago

Below Python Script I have used for my purpose. When I am in Firefox window it has failed to execute the javascript and thronging error. The error text also given below.

+++++++++++++++Python Script++++++++++++++++

import win32gui, win32process, psutil import js2py

def active_window_process_name(): try: pid = win32process.GetWindowThreadProcessId(win32gui.GetForegroundWindow()) return(psutil.Process(pid[-1]).name()) except: pass

windowTile = ""; while ( True ) : newWindowTile = win32gui.GetWindowText(win32gui.GetForegroundWindow()); if active_window_process_name() != 'firefox.exe': if( newWindowTile != windowTile ) : windowTile = newWindowTile ; print(active_window_process_name(),windowTile ); elif active_window_process_name() == 'firefox.exe': if(newWindowTile != windowTile ) : windowTile = newWindowTile ; window = win32gui.GetWindowText(win32gui.GetForegroundWindow()) ; x=js2py.eval_js('var a = console.log(window.location.href); a'); print(x)

================The error message ==================== Traceback (most recent call last): File "D:\pyth\copy active window process name.py", line 22, in x=js2py.eval_js('var a = console.log(window.location.href); a'); File "D:\Python2020\lib\site-packages\js2py\evaljs.py", line 115, in eval_js return e.eval(js) File "D:\Python2020\lib\site-packages\js2py\evaljs.py", line 204, in eval self.execute(code, use_compilation_plan=use_compilation_plan) File "D:\Python2020\lib\site-packages\js2py\evaljs.py", line 199, in execute exec (compiled, self._context) File "", line 2, in File "D:\Python2020\lib\site-packages\js2py\base.py", line 949, in call return self.call(self.GlobalObject, args) File "D:\Python2020\lib\site-packages\js2py\base.py", line 1464, in call return Js(self.code(*args)) File "D:\Python2020\lib\site-packages\js2py\host\jseval.py", line 45, in Eval executor(py_code) File "D:\Python2020\lib\site-packages\js2py\host\jseval.py", line 51, in executor exec (code, globals()) File "", line 2, in File "D:\Python2020\lib\site-packages\js2py\base.py", line 321, in get raise MakeError('TypeError', js2py.internals.simplex.JsException: TypeError: Undefined and null dont have properties (tried getting property 'href')

worstperson commented 3 years ago

This is a JavaScript interpreter. If you want to interface with and run code in a browser, use something like Selenium instead.