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?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Shuo
  • 8,447
  • 4
  • 30
  • 36

1 Answers1

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.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Is that possible execute_async_script returned after execute_script? Then execute_script will encounter an error? – Shuo Aug 10 '15 at 06:02
  • @Shuo well, you need to make sure the required library is loaded before the usage.. – alecxe Aug 10 '15 at 13:25