Click the HtmlUnit Button

I am trying to send a message to www.meetme.com but cannot figure out how to do this. I can enter a message in the comments area, but clicking the submit button does nothing. What am I doing wrong? When I log in and click the "Login" button, the page changes and everything is in order. Anyone have any ideas or tips?

HtmlPage htmlPage = null; HtmlElement htmlElement; WebClient webClient = null; HtmlButton htmlButton; HtmlForm htmlForm; try{ // Create and initialize WebClient object webClient = new WebClient(BrowserVersion.FIREFOX_17 ); webClient.setCssEnabled(false); webClient.setJavaScriptEnabled(false); webClient.setThrowExceptionOnFailingStatusCode(false); webClient.setThrowExceptionOnScriptError(false); webClient.getOptions().setThrowExceptionOnScriptError(false); webClient.getOptions().setUseInsecureSSL(true); webClient.getCookieManager().setCookiesEnabled(true); /*webClient.setRefreshHandler(new RefreshHandler() { public void handleRefresh(Page page, URL url, int arg) throws IOException { System.out.println("handleRefresh"); } });*/ htmlPage = webClient.getPage("http://www.meetme.com"); htmlForm = htmlPage.getFirstByXPath("//form[@action='https://ssl.meetme.com/login']"); htmlForm.getInputByName("username").setValueAttribute(" blah@gmail.com "); htmlForm.getInputByName("password").setValueAttribute("blah"); //Signing in htmlButton = htmlForm.getElementById("login_form_submit"); htmlPage = (HtmlPage) htmlButton.click(); htmlPage = webClient.getPage("http://www.meetme.com/member/1234567890"); System.out.println("BEFORE CLICK"); System.out.println(htmlPage.asText()); //type message in text area HtmlTextArea commentArea = (HtmlTextArea)htmlPage.getFirstByXPath("//textarea[@id='profileQMBody']"); commentArea.setText("Testing"); htmlButton = (HtmlButton) htmlPage.getHtmlElementById("profileQMSend"); htmlPage = (HtmlPage)htmlButton.click(); webClient.waitForBackgroundJavaScript(7000); //The print is exactly the same as the BEFORE CLICK print System.out.println("AFTER CLICK"); System.out.println(htmlPage.asText()); }catch(ElementNotFoundException e){ e.printStackTrace(); }catch(Exception e){ e.printStackTrace(); } 
+4
source share
1 answer

Unaware of the webpage you are accessing, you simply cannot execute an AJAX request with JavaScript disabled. If the change does not succeed, you will have to debug, but make sure JavaScript is enabled.

Also, make sure that you use HtmlUnit 1.12 and update all obsolete methods in your code.

By the way, I would also recommend disabling JavaScript warnings. Check this answer to find out how you can do this.

+3
source

Source: https://habr.com/ru/post/1499995/


All Articles