问题
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