title: How to use HtmlUnit in Java?
body: <p>I'm trying to use HtmlUnit in Java to log into a website. First i enter the user name then password. After that i need to select an option from a dropdown box. entering the user and password seemed to have worked but when i try to select the item from the drop down box i get errors. Can anyone help me fix this? My code is as follows:</p>
  
  <pre><code>import com.gargoylesoftware.htmlunit.WebClient;
  import com.gargoylesoftware.htmlunit.html.HtmlElement;
  import com.gargoylesoftware.htmlunit.html.HtmlOption;
  import com.gargoylesoftware.htmlunit.html.HtmlPage;
  import com.gargoylesoftware.htmlunit.html.HtmlSelect;
  
  
  public class homePage {
    public static void main(String[] args) throws Exception {
  
      final WebClient webClient = new WebClient();
      final HtmlPage page = webClient.getPage("website name here");
      HtmlElement usrname = page.getElementByName("username");
      usrname.click();
      usrname.type("myusername");
      HtmlElement psswrd = page.getElementByName("password");
      psswrd.click();
      psswrd.type("mypassword");
      HtmlSelect select = (HtmlSelect) page.getElementById("cmbProducts");
      HtmlOption option = select.getOptionByValue("ITDirect");
      select.setSelectedAttribute(option, true);
      HtmlElement signin = page.getElementByName("SignIn");
      signin.click();
      System.out.println(page.getTitleText());
      webClient.closeAllWindows();
    }
  }
  </code></pre>