title: Selenium not selecting select option due to javascript call back
body: <p>I have read through answers to similar questions on stackoverflow, but none of the answers work for me. The specific difference to most answers is the javascript callback in the select element. I have tried Select object, locators using index, value and text, none will select the correct option, all due to the javascript callback I believe</p>
  
  <p>This is the element I am trying to select from:</p>
  
  <pre><code>&lt;select name="ctl0" onchange="javascript:setTimeout('__doPostBack(\'ctl0\',\'\')', 0)" id="ctl0" style="width:205px;margin-left:30px;"&gt;
      &lt;option value="0"&gt;Option 0&lt;/option&gt;
      &lt;option selected="selected" value="1"&gt;Option 1&lt;/option&gt;
      &lt;option value="2"&gt;Option 2&lt;/option&gt;
  
  &lt;/select&gt;
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>driver.findElement(By.id("ctl0")).click();   
  driver.findElement(By.xpath("//select[@id=\"ctl0\"]/option[@value=\"1\"]")).click();
  </code></pre>
  
  <p>An alternate approach with no difference in outcome:</p>
  
  <pre><code>driver.findElement(By.id("ctl0")).click();
  Select select = new Select(driver.findElement(By.id("ctl0")));
  WebElement elem = select.getOptions().get(1);
  System.out.println(elem.getText());
  elem.click();
  </code></pre>
  
  <p>Before you answer, I have to click both of these according to others, because the callback seems to fool a Select object, thus the explicit click and XPath locators.</p>
  
  <p>The selection of the <em>select</em> control works in both case, the selection of the <em>option</em> seems to work, but the click does not cause it to be selected. </p>
  
  <p>I tried the option below for javascript, same issue</p>
  
  <pre><code>    WebElement el = driver.findElement(By.id("ctl0"));
      String jsScript = "showDropdown = function (element) "
              + "{"
              + "    var event; "
              + "    event = document.createEvent('MouseEvents'); "
              + "    event.initMouseEvent('mousedown', true, true, window, 1, 0,0,0,0,false,false,false,false,0,null); "
              + "    element.dispatchEvent(event); "
              + "}; "
              + "showDropdown(arguments[0]);";
  
      ((JavascriptExecutor)driver).executeScript(jsScript,el);
  
      WebElement elem = el.findElement(By.xpath(".//option[@value = '1']"));
      System.out.println("Option visible text is " + elem.getText());
      elem.click();
  </code></pre>
  
  <p>The site is not public and I have no control over it. Note that the initMouseEvent as specified in the solution below does not have all required arguments. I think I have them right. initMouseEvent is now deprecated too.</p>
  
  <p>I had originally used Selenium IDE to record the actions. This is how it exports it for Java:</p>
  
  <pre><code>new Select(driver.findElement(By.id("ctl0"))).selectByVisibleText("Option 1");
  driver.findElement(By.cssSelector("option[value=\"1\"]")).click();
  </code></pre>
  
  <p>After reading this - <a href="https://stackoverflow.com/questions/2598230/preserve-onchange-for-a-dropdown-list-when-setting-the-value-with-javascript">Preserve onchange for a dropdown list when setting the value with Javascript</a><a href="https://stackoverflow.com/questions/2598230/preserve-onchange-for-a-dropdown-list-when-setting-the-value-with-javascript"></a></p>
  
  <p>I tried this (also with the function undecorated with window prefix): </p>
  
  <pre><code>    new Select(driver.findElement(By.id("ctl0"))).selectByValue("1");
      String jsScript = "window.__doPostBack(arguments[0],'');";
  
      ((JavascriptExecutor)driver).executeScript(jsScript,"ctl0");
  </code></pre>
  
  <p>But get this:</p>
  
  <pre><code>org.openqa.selenium.WebDriverException: TypeError: window.__doPostBack is not a function (WARNING: The server did not provide any stacktrace information)
  </code></pre>
  
  <p>Ideas?</p>