title: How can I capture the "load" event by adding my own listener through ExecuteScript?
body: <p>I am trying to catch the <code>load</code> (not <code>onload</code>) event with Selenium.</p>
  
  <p>My code looks like this (see comments) :</p>
  
  <pre><code>// d is my webdriver
  // go to url
  d.Navigate().GoToUrl(url);
  WebDriverWait wait = new WebDriverWait(d, TimeSpan.FromSeconds(30));
  
  // create a function that add an element with id="my_123456789"
  string javascript = 
     "my_readyFCT = function(){"
       + "alert('READY');"
       + "var e = document.createElement('div');"
       + "e.id = 'my_123456789';"
       + "document.body.appendChild(e);"
     + "};"
  // call the function once 'load' event is fired
     + "window.addEventListener('load', my_readyFCT, false);"
  
  ((IJavaScriptExecutor)d).ExecuteScript(javascript);
  
  // Selenium wait until the element with id "my_123456789" exists
  wait.Until&lt;IWebElement&gt;(ExpectedConditions.ElementExists(By.Id("my_123456789")));
  </code></pre>
  
  <p>I always get a timeout exception (30 seconds) and I can't see what I'm doing wrong.</p>
  
  <p>Any idea ?</p>
  
  <p><strong>Note</strong> : I tried to execute the function <code>my_readyFCT</code> on it's own and it created the element with the correct id inserted into the DOM.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here is the javascript :</p>
  
  <pre><code>my_readyFCT = function() {
     alert('READY');
     var e = document.createElement('div');
     e.id = 'my_123456789';
     document.body.appendChild(e);
  };
  // call the function once 'load' event is fired
  window.addEventListener('load', my_readyFCT, false);
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I must say I found a way to avoid the usual behavior of <code>GoToUrl</code> (which is to way for page load) :</p>
  
  <pre><code>var fb = new FirefoxBinary();
  fb.Timeout = TimeSpan.FromMinutes(4);
  FirefoxProfile fp = new FirefoxProfile();
  // 'unstable' allow us to avoid the implicit wait in GoToUrl
  fp.SetPreference("webdriver.load.strategy", "unstable");
  d = new FirefoxDriver(fb, fp, TimeSpan.FromMinutes(1));
  </code></pre>
  
  <p>And when I want to change url :</p>
  
  <pre><code>d.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(0));
  try {
     d.Navigate().GoToUrl(url);
  }
  catch(WebDriverException) /* will be thrown everytime */ 
  { }
  
  WebDriverWait wait = new WebDriverWait(d, TimeSpan.FromSeconds(30));
  /*
  *  ...
  *   ETC. See code above
  *  ...
  */
  </code></pre>