From: Emmanuel M. <nue...@gm...> - 2019-02-03 14:21:32
|
Hi, My username is rich25 I've used HtmlUnit for my other program and it working perfectly. I have this problem now with this program. I'm accessing this website https://www.betexplorer.com and I want to click the top right login link written "login". After I click the link, the pop up login form appears and I want to enter the login credentials and click the login button, but I can't seem to find this JavaScript pop up login form with HtmlUnit. Here is my code snipets below: import com.gargoylesoftware.htmlunit.BrowserVersion; import com.gargoylesoftware.htmlunit.WebClient;import com.gargoylesoftware.htmlunit.WebWindowEvent; import com.gargoylesoftware.htmlunit.html.DomElement; import com.gargoylesoftware.htmlunit.WebWindowListener; import com.gargoylesoftware.htmlunit.html.HtmlButton; import com.gargoylesoftware.htmlunit.html.HtmlElement; import com.gargoylesoftware.htmlunit.html.HtmlForm; import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput; import com.gargoylesoftware.htmlunit.html.HtmlTextInput; public class ExplorerHtmlUnit { public static void main(String[] args) throws Throwable { WebClient webClient; webClient = new WebClient(); webClient = new WebClient(BrowserVersion.CHROME); webClient.getOptions().setThrowExceptionOnScriptError(false); webClient.getOptions().setJavaScriptEnabled(true); java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF); HtmlPage explorerPage = webClient.getPage("https://www.betexplorer.com"); System.out.println("Explorer Website Found"); //I'm not sure if the login button on top is clicked at this point until the "following code works" List<DomElement> spans = explorerPage.getByXPath("//a[@class='clientmenu__item__in js-window-trigger'][contains(text(),'Login')]"); for (DomElement ele : spans) { if(ele.getAttribute("class").equals("login")) { //ele.click(); } } System.out.println("login icon found and clicked"); //sleep for 5 seconds for javascript form to load Thread.sleep(5000); //the following code mentioned above webClient.addWebWindowListener(new WebWindowListener() { public void webWindowOpened(WebWindowEvent event) { windows.add(event.getWebWindow()); } @Override public void webWindowClosed(WebWindowEvent arg0) { // TODO Auto-generated method stub } @Override public void webWindowContentChanged(WebWindowEvent arg0) { // TODO Auto-generated method stub } }); WebWindow latestWindow = windows.getLast(); HtmlPage urPage = (HtmlPage) latestWindow.getEnclosedPage(); HtmlTextInput submitUsername = (HtmlTextInput) explorerPage.getByXPath("//input[@id='login_nick']"); submitUsername.setValueAttribute("username"); System.out.println("UserName Found And Entered"); HtmlPasswordInput submitPassword = (HtmlPasswordInput) explorerPage.getByXPath("//input[@id='login_nick']"); submitPassword.setValueAttribute("password"); System.out.println("Password Found And Entered"); HtmlButton loginButton = (HtmlButton) explorerPage.getByXPath("//html[1]/body[1]/div[6]/div[1]/div[1]/div[1]/div[1]/form[1]/div[1]/div[3]/button[1]"); loginButton.click(); System.out.println("Login Button Found And Clicked"); webClient.close(); }} What should I do with the pop up window to enter the login information and click the login button. |