title: How to handle alert with HtmlUnitDriver in java?
body: <p>I'm trying to handle alerts event with HtmlUnitDriver, but I have some problems, and I would like to understand why. 
  Here is the java code:</p>
  
  <pre><code>HtmlUnitDriver browser = new HtmlUnitDriver(true);
  browser.get("http://localhost:8001/index.html");
  browser.findElementById("myButton").click();
  
  try {
      WebDriverWait wait = new WebDriverWait(browser, 2);
      wait.until(ExpectedConditions.alertIsPresent());
      Alert alert = browser.switchTo().alert();
      alert.accept();
      System.out.println("ALERT");
  } catch (Exception e) {
      System.out.println(e.getMessage());
      System.out.println("NO ALERT");
  }
  String htmlContent = browser.getPageSource();
  System.out.println(htmlContent);
  
  browser.close();    
  </code></pre>
  
  <p>and this is the html code:
  index.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
      &lt;title&gt;&lt;/title&gt;
      &lt;meta charset="utf-8" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1"&gt;
          &lt;input id="username" name="username" /&gt;
          &lt;button id="myButton" type="button" value="Page2"&gt;Go to Page2&lt;/button&gt;
      &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  
  &lt;script&gt;
      document.getElementById("myButton").onclick = function () {
          location.href = "page2.html";
      };
  &lt;/script&gt;   
  </code></pre>
  
  <p>page2.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;
      &lt;title&gt;Page2&lt;/title&gt;
      &lt;meta charset="utf-8" /&gt;
  &lt;/head&gt;
  &lt;body onload="myfunction('hello2')"&gt;
      &lt;p id="result"&gt;&lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  &lt;script&gt;
      function myfunction(data) {
          document.getElementById('result').innerHTML = data
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>The output in the console is :</p>
  
  <pre><code>NO ALERT
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;html&gt;
    &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;
      &lt;title&gt;
        Page2
      &lt;/title&gt;
      &lt;meta charset="utf-8"/&gt;
    &lt;/head&gt;
    &lt;body onload="myfunction('hello2')"&gt;
      ?
  
  
      &lt;p id="result"&gt;
        hello2
      &lt;/p&gt;
      &lt;script&gt;
  //&lt;![CDATA[
  
      function myfunction(data) {
          document.getElementById('result').innerHTML = data
      }
  
  //]]&gt;
      &lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Looking at the output, it seems to be a little different from the source code, and about this I have few questions.
  why the alert on page2.html isn't detected?
  Why are there some extra characters, such as "?" and "// &lt; ! [CDATA["? How can I avoid them?</p>
  
  <p>I'm trying to handle alerts, and I'm at the beginning, so any suggestions will be appreciate.</p>