1

Hi I'm currently trying to get a script that will fill in login details on a site and the submit the form and allow me to gather information from the next page. So far I've managed to fill in the form but I can't get it to submit. I've tried the .click on the button and submit() but neither work. So now I've turned to JavaScript to activate the button. Unfortunately this gives me the error below. I've tried to override the error but I may have done that wrong. Does anyone know how I can submit the form?

This is the error I believe brings my code to a halt

WARNING: Obsolete content type encountered: 'application/x-javascript'.
Exception in thread "main" org.openqa.selenium.WebDriverException: com.gargoylesoftware.
htmlunit.ScriptException: ReferenceError: "console" is not 
defined. (https://extbasicph05.podc.sl.edst.ibm.com/FFAMobileRelay/js/ion.sound.min.js#2)

This is my code

public class Main {

public class MyHtmlUnitDriver extends HtmlUnitDriver {


     @Override
        protected WebClient modifyWebClient(WebClient client) {
            //currently does nothing, but may be changed in future versions
            WebClient modifiedClient = super.modifyWebClient(client);

            modifiedClient.getOptions().setThrowExceptionOnScriptError(false);
            return modifiedClient;
        }
    }

 public static void main(String[] args) {


        WebDriver driver = new HtmlUnitDriver(true);

        driver.get("url");


        WebElement usr = driver.findElement(By.id("username"));
        WebElement psd = driver.findElement(By.id("password"));
        WebElement btt = driver.findElement(By.id("btnLogin"));

        usr.sendKeys("username");
        psd.sendKeys("password");


        String eventScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('"
                 + "click"
                 + "', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('"
                 + "onclick"+ "');}";
               JavascriptExecutor js = (JavascriptExecutor) driver;
               js.executeScript(eventScript, btt);



        System.out.println("Page title is: " + driver.getTitle());
    }

}
mjturner
  • 1,035
  • 7
  • 16
user1261404
  • 305
  • 2
  • 3
  • 10
  • Can you get this to work on a real browser, Firefox for example? – SiKing Jul 01 '15 at 21:50
  • When I ran the script using chrome instead of HTMLUnit it worked fine with just a .Click but not the javascript – user1261404 Jul 01 '15 at 21:56
  • When I use enter or ,click on HTMUnit it can''t locate the element to submit – user1261404 Jul 01 '15 at 21:59
  • 1
    For this case, JS is definitely the wrong way to go. `HtmlUnitDriver` is usable only for the most basic websites. Anything with any amount of AJAX is simply not going to work. If you need a headless browser look into [tag:phantomjs] or [tag:xvfb]. – SiKing Jul 01 '15 at 22:01
  • Is the submit button displayed initially? Or is it not displayed / updated until the login details are entered? If this is the case it might be trying to click the button too fast when you use .click and you might need to add a short wait of some sort after the login credentials have been entered – unsensibled Jul 01 '15 at 22:06
  • Please use latest version, you can use HtmlUnitDriver(BrowserVersion.CHROME), and you can post your complete case with the URL used. – Ahmed Ashour Jul 01 '15 at 22:09
  • @ahmed I have tried using .chrome but it say javascript is not activaed. – user1261404 Jul 01 '15 at 22:14
  • That method is undefined – user1261404 Jul 01 '15 at 22:18
  • Ah Great that seems to make the JavaS active but it still doesn't seem to submit. Is my java scripting correct? – user1261404 Jul 01 '15 at 22:22
  • You should not use JavaScript to submit. `setThrowExceptionOnScriptError` hides error from you. Please edit your post with the complete case with URL with clicking without JavaScript, so others can reproduce the issue. – Ahmed Ashour Jul 01 '15 at 23:02
  • 1
    I got a similar problem yesterday, when I tried to use HtmlUnitDriver. The problem was when I used the driver with javascript, then the form on the site was different because it recognize that I'm using an old browser. And when I turned on the BrowserVersion, the javascript didn't work. So I recommend to try a different headless browser like @SiKing said. – peetya Jul 02 '15 at 07:07

0 Answers0