How do I execute a function from external Javascript file using Python and Selenium?

柔情痞子 提交于 2021-02-11 13:32:58

问题


I am new to Selenium and am stuck trying to get it to automatically open links that look like this:

<a href='javascript:void(0)' onclick=javascript:myFunction(argument1, argument2)>Click here</a>

The myFunction function is in an external .js file. I couldn't fetch it from the server (this is material for another question) so I copied and pasted it into a text file that I have saved on my hard drive. I load and run it with

with open ('MY_PATH', 'r') as f:
     ext_js = f.read()

driver.execute_script(ext_js)

which runs without a problem. But then when I try to run

driver.execute_script("myFunction(argument1, argument2)")

I get the following error:

JavascriptException: Message: javascript error: myFunction is not defined

Shouldn't it have been imported when I have executed the external .js file? How do I get my code to see it?

来源:https://stackoverflow.com/questions/65452211/how-do-i-execute-a-function-from-external-javascript-file-using-python-and-selen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!