0

Trying to use HTMLUnit on a page called TimeEdit which is used to make schedule mainly for schools. I want to add a search term ("DV1431") in the inputfield here: TimeEdit

And then I want to submit this some way. I have read that you can trigger JavaScript with HTMLUnit and also that you can use buttons and even create "fake ones". But I'm not sure what is the best way in my case.

I have tried a solution that I found here on Stackoverflow: Youtube-example This one worked, but on my page TimeEdit there is no HTML-form that I can use. Instead there is just a class called searchButtons where the submit-button and input field is located. Here is my example-code:

    WebClient webClient = new WebClient(BrowserVersion.CHROME);
    webClient.getOptions().setCssEnabled(false);
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setUseInsecureSSL(true);
    webClient.getCookieManager().setCookiesEnabled(true);

    //Get the page
    HtmlPage currentPage = webClient.getPage("https://se.timeedit.net/web/bth/db1/sched1/ri1Q7.html");

    //Just for testing purpose
    System.out.println(currentPage.getUrl());

    // Get form where submit button is located
    // But on this page there is no form, just a class called searchButtons
   HtmlForm searchForm = (HtmlForm) currentPage.getElementById("ffsearchname");

    // Get the input field.
    HtmlTextInput searchInput = (HtmlTextInput) currentPage.getElementById("ffsearchname");

    // Insert the search term.
    searchInput.setText("DV1431");

    // Workaround: create a 'fake' button and add it to the form.
    HtmlButton submitButton = (HtmlButton) currentPage.createElement("button");
    submitButton.setAttribute("type", "submit");
    searchForm.appendChild(submitButton);

    // Workaround: use the reference to the button to submit the form. 
    HtmlPage newPage = submitButton.click();

    //Testing purpose
    System.out.println(newPage.getUrl());

How can I access the submit-button and input-field when it's not in a form and how do I submit when I can set my searchterm in the inputfield?

Community
  • 1
  • 1
anderssinho
  • 298
  • 2
  • 7
  • 21

1 Answers1

1

No idea why you try to insert a button and why you expect that button might be of any use. If you like to automate some Html pages you need some basic understanding of the way html and in your case also javascript works.

    // no need to set any options just use the default
    WebClient webClient = new WebClient(BrowserVersion.CHROME);

    // Get the page and wait for the javacode that will start with an minimal delay
    // doing something like setTimeout(init(), 10); is a common trick done by some js libs
    HtmlPage currentPage = webClient.getPage("https://se.timeedit.net/web/bth/db1/sched1/ri1Q7.html");
    currentPage.getEnclosingWindow().getJobManager().waitForJobsStartingBefore(100);

    // Get the input field.
    HtmlTextInput searchInput = (HtmlTextInput) currentPage.getElementById("ffsearchname");

    // Insert the search term.
    searchInput.setText("DV1431");

    // the output
    DomElement output = currentPage.getElementById("objectsearchresult");
    System.out.println("- before -------------------------------------------------------------------");
    System.out.println(output.asText());
    System.out.println("----------------------------------------------------------------------------");

    // try to find the button
    for (final DomElement elem : currentPage.getElementsByTagName("input")) {
        if ("Sök".equals(((HtmlInput) elem).getValueAttribute())) {
            // click and again wait for the javascript
            currentPage = elem.click();
            currentPage.getEnclosingWindow().getJobManager().waitForJobsStartingBefore(100);

            System.out.println();
            output = currentPage.getElementById("objectsearchresult");
            System.out.println("- after --------------------------------------------------------------------");
            System.out.println(output.asText());
            System.out.println("----------------------------------------------------------------------------");
            break;
        }
    }

BTW: this code is only a sample to prove HtmlUnit will be able to automate your page. For finding controls you should study the various options the api offers and choose the appropriate one for your use case (maybe XPath).

RBRi
  • 2,704
  • 2
  • 11
  • 14
  • I read some about xPath and tried to use it, but never got it to work. Can find the specific button using it, but can't "click" it then since it's in a List then. Tried to cast it but it didn't work. I'm not sure that my code is anyway near right. I just read some examples and tried to use it. But every example I tried has a form on the HTML-page. And then it's no problem to input in textfield, find button and click it. But on TimeEdit they have something called "fancytypeselector" in a class called searchButtons instead. And that's what's causing all problems for me right now.. – anderssinho Jan 04 '17 at 13:11
  • No it doesn't seem to be working. As far as I can see when I test it, it sets the text in the inputfield, but it doesn't seem to be doing the click. – anderssinho Jan 05 '17 at 10:01
  • In the for-loop we seem to find the button, but when I try to find the course in objectbasket class there is nothing inside it. That's why I think the click() doesnt actully do anything. If you use the page manually you will see that when you click the "Sök" the course will appear in a list below to the left (If the coursecode is valid). Sorry if my explanations isn't the best – anderssinho Jan 05 '17 at 10:10
  • 1
    Ok, you are right i was a bit confused. Have done some fixes to the code and now it works fine for me - hopefully for you also. – RBRi Jan 05 '17 at 19:49
  • That pience of code seems to do the trick, thx mate. Can i just also ask if I can use kinda the same code to press the button called "Visa Schema", of course with some modification? – anderssinho Jan 06 '17 at 09:19
  • 1
    Maybe :-), just try it and if it fails i can try to support you. If you like to test the page it might be an option to have a look at www.wetator.org. This tool provides a simple, more intuitive way to access the controls and does a lot of magic in the background to hide all the magic required to work with this kind of apps. – RBRi Jan 06 '17 at 09:39
  • Solved it now thanks with your help :) Thank you very much! – anderssinho Jan 06 '17 at 12:58