title: How to execute js in selenium?
body: <p>Selenium WebDriver Java</p>
  
  <p>Anyone knows how to execute this script <code>js.executeScript("window.addEventListener('load',function(){document.getElementsByTagName('script')[1].src='google.com'})");</code></p>
  
  <p>or</p>
  
  <pre><code>js.executeScript("window.load = function()
  {document.getElementsByTagName('script')[1].src='google.com'})");
  </code></pre>
  
  <p>This script return null. 
  Eclipse says: Exception in thread "main" java.lang.NullPointerException
      at autotesting.itacademy.main(itacademy.java:58)</p>
  
  <p>The main code here it is: </p>
  
  <pre><code>package autotesting;
  
  import org.openqa.selenium.JavascriptExecutor;
  import org.openqa.selenium.WebDriver;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Set;
  import java.util.concurrent.TimeUnit;
  
  import org.apache.commons.io.FileUtils;
  import org.openqa.selenium.*;
  import org.openqa.selenium.firefox.FirefoxDriver;
  import org.openqa.selenium.logging.LogEntries;
  import org.openqa.selenium.support.ui.Select;
  import org.openqa.selenium.By;
  import org.openqa.selenium.JavascriptExecutor;
  import org.openqa.selenium.OutputType;
  import org.openqa.selenium.TakesScreenshot;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.WebElement;
  
  //for read and write from file
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.FileWriter;
  import java.util.Scanner;
  
  public class itacademy {
  
       private static WebDriver driver = null;
  
       public static JavascriptExecutor js = (JavascriptExecutor) driver;
  
      public static void main(String[] args) throws IOException {
          // TODO Auto-generated method stub
  
          JavascriptExecutor js = (JavascriptExecutor) driver;
  
          driver = new FirefoxDriver();
          driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
          driver.manage().window().maximize();        
          driver.get("http://office.co.uk/");
  
          js.executeScript("window.addEventListener('load',function(){document.getElementsByTagName('script')[1].src='google.com'})");
              }}
  </code></pre>
  
  <p>Thanks in advance</p>