Tuesday, 30 August 2016

htmlunit click button without form | click button htmlunit | htmlunit click button example | click home button 3 ways

htmlunit click button without form | click button htmlunit | htmlunit click button example | click home button 3 ways

Requrie library : HtmlUnit

There are three different way to set text on textbox and clicked on button.

you just pass your HtmlPage and get new HtmlPage will contains like,
whenever you manually clicked on browser to click button and redirect page will display.


 
Screen
html code (only textbox and scheduled job button)

<tr>
    <td>
        <br>
        <div class="actionsmenu">
            <span class="checkbox-set">
                <input id="SearchInput" type="text" name="ItemContent" value="">
            </span>
            <a href="http://scrapemania.blogspot.in/sch?softinttype=1">Clear options </a>
        </div>
    </td>
</tr>

<tr>
    <td>
        <br>
        <div class="actionsmenu">
            <span class="advers">
                <button id="searchBtnLowerLnk" class="datasubmition" name="ItemFiller" type="submit" onclick="return false;">Scheduled Job</button>
            </span>
            <a href="http://scrapemania.blogspot.in/sch?softinttype=1">Clear options </a>
        </div>
    </td>
</tr>


There are three differnt methods you just pass HtmlPage and get Clicked new HtmlPage.

//First Method
public HtmlPage getPageAfterButtonClickFirstWay(HtmlPage page) throws IOException {

        //set Text whatever you want
        String text = "set text whatever you want";

        //getting TextBox
        HtmlTextInput txtfield = page.getElementById("SearchInput", false);

        //getting Button
        HtmlSubmitInput btn = null;
        if (page.getElementsByTagName("input") != null) {
            DomNodeList<DomElement> inputs = (DomNodeList<DomElement>) page.getElementsByTagName("input");
            for (DomElement input : inputs) {
                if (input.getAttribute("class").equals("datasubmition")) {
                    btn = (HtmlSubmitInput) input;
                }
            }
        }

        // Set the Textfield=text
        txtfield.setValueAttribute(text);

        //press Search Button
        return btn.click();
    }

//Second Method
    public HtmlPage getPageAfterButtonClickSecondWay(HtmlPage page) throws IOException {

        //set Text whatever you want
        String text = "set text whatever you want";

        //getting TextBox
        HtmlTextInput txtfield = null;
        if (page.getElementsByTagName("input") != null) {
            DomNodeList<DomElement> inputs = (DomNodeList<DomElement>) page.getElementsByTagName("input");
            for (DomElement input : inputs) {
                if (input.getAttribute("type").equals("text")) {
                    txtfield = (HtmlTextInput) input;
                }
            }
        }

        //getting Button
        HtmlSubmitInput btn = null;
        if (page.getElementByName("ItemFiller") != null) {
            DomNodeList<DomElement> inputs = (DomNodeList<DomElement>) page.getElementByName("ItemFiller");
            for (DomElement input : inputs) {
                if (input.getAttribute("class").equals("datasubmition")) {
                    btn = (HtmlSubmitInput) input;
                }
            }
        }

        // Set the Textfield=text
        txtfield.setValueAttribute(text);

        //press Search Button
        return btn.click();
    }

//Third Method
    public HtmlPage getPageAfterButtonClickThirdWay(HtmlPage page) throws IOException {

        //set Text whatever you want
        String text = "set text whatever you want";

        //getting TextBox
        HtmlTextInput txtfield = page.getElementByName("ItemContent");

        //getting Button
        HtmlSubmitInput btn = null;
        if (page.getElementByName("ItemFiller") != null) {
            DomElement input = (DomElement) page.getElementByName("ItemFiller");
            btn = (HtmlSubmitInput) input;
        }

        // Set the Textfield=text
        txtfield.setValueAttribute(text);

        //press Search Button
        return btn.click();
    }



1 comment:

  1. Great post, you have pointed out some excellent points, I as well believe this is a very superb website.
    Search Button

    ReplyDelete