title: How do I load a javascript file into the DOM using selenium?
body: <p>I'm using Selenium WebDriver to try to insert an external javascript file into the DOM, rather than type out the entire thing into executeScript.</p>
  
  <p>It looks like it properly places the  node into the DOM, but then it just disregards the source, i.e. the function on said source js file doesn't run.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import org.openqa.selenium.By;
  import org.openqa.selenium.JavascriptExecutor;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.WebElement;
  import org.openqa.selenium.firefox.FirefoxDriver;
  
  public class Example  {
      public static void main(String[] args) {
          WebDriver driver = new FirefoxDriver();
          driver.get("http://google.com");
          JavascriptExecutor js = (JavascriptExecutor) driver;
          js.executeScript("document.getElementsByTagName('head')[0].innerHTML += '&lt;script src=\"&lt;PATH_TO_FILE&gt;\" type=\"text/javascript\"&gt;&lt;/script&gt;';");
      }
  }
  </code></pre>
  
  <p>The code of the javascript file I am linking to is</p>
  
  <pre><code>alert("hi Nate");
  </code></pre>
  
  <p>I've placed the js file on my localhost, I called it using file:///, and I tried it on an external server. No dice.</p>
  
  <p>Also, in the Java portion, I tried appending 'scr'+'ipt' using that trick, but it still didn't work. When I inspect the DOM using Firefox's inspect element, I can see it loads the script node properly, so I'm quite confused.</p>
  
  <p>I also tried this solution, which apparently was made for another version of Selenium (not webdriver) and thus didn't work in the least bit: <a href="https://stackoverflow.com/questions/7675492/load-an-external-js-file-containing-useful-test-functions-in-selenium">Load an external js file containing useful test functions in selenium</a></p>