1

Followed the start guide, I can run a javascript snippet by using driver.execute(). How can I run external javascript files, which load some external modules itself.

Possible ways I can come up with:

  1. Concatenate all required files into a single large file and then load it into a string and run it with driver.execute(). Possibly with help of a minifier.

  2. Execute a small snippet to load all required js files.

  3. Maybe modify the html before browser render it?

Any suggestions?

1 Answer 1

1

You can actually load scripts dynamically via execute_script(). Here is an example use case where jquery library is dynamically loaded to support HTML5 drag&drop simulation:

The key functionality is the javascript code that is executed via execute_async_script() (taken from here) that adds a script element to head via document.createElement() on the fly.

The first link has a working example in Python, the second one has it in Java.

Sign up to request clarification or add additional context in comments.

2 Comments

Is that possible execute_async_script returned after execute_script? Then execute_script will encounter an error?
@Shuo well, you need to make sure the required library is loaded before the usage..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.