Rectangle 27 5

try this if you are unable to click on the button

HtmlElement htmlElement = page.getFirstByXPath("//*[@id=\"ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_btnSearch_Table\"]/tbody/tr/td[2]");
   htmlElement.click();

if this is not what you are looking for, and you already handled the click part i suggest try wait for the page and javascript to load and execute.

webClient.waitForBackgroundJavaScript(1000);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.setAjaxController(new AjaxController(){
@Override
public boolean processSynchron(HtmlPage page, WebRequest request, boolean async)
{
return true;
}
});
JavaScriptJobManager manager = page.getEnclosingWindow().getJobManager();
while (manager.getJobCount() > 0) {
Thread.sleep(100);
}

java - How to click on a javascript button in htmlunit - Stack Overflo...

java javascript html htmlunit
Rectangle 27 1

The problem is not in the code used to click on the link. It's in the JavaScript executed when the link is clicked. Either the JavaScript is buggy, or the JavaScript interpreter used by HtmlUnit has a problem running it.

The problem seems to be with a parameter x which has the Infinity value during the execution.

Thats not good. Am I totally out of luck or is there something else I can do? I have done alot of work and its bad that the next page click can kill this project. Is there a way to make the javascript behave better? or some setting to let htmlunit's javascript interpreter behave better? or even load another javascript engine in it(google's v8, mozilla spidermonkey,etc..)

I FIGURED IT OUT!! YAY. I was worried my project would fail but basically I had to set webclient = WebClient(BrowserVersion.FIREFOX_3_6) I set it to IE7 before and it wasn't working. I guess the site behaves differently with different browser heading..

java - struggling to click on link within htmlunit - Stack Overflow

java screen-scraping web-scraping htmlunit
Rectangle 27 1

form.getElementById("button")
getInputByName
  • Use Thread.sleep to wait (around 350 ms seems to be the magic number) for the effect to take place

Even with this, I still see non-determinstic test failures because the effects of the click sometimes just don't seem to happen.

I've tried the Element stuff but it does not change anything. JS still does't run. Sleep did't work ass well.

form
HtmlPage

java - Clicking a javascript button with HtmlUnit - Stack Overflow

java javascript button htmlunit
Rectangle 27 1

The click function is in the table element, try this(didn't try on my own):

HtmlElement theElement5 = (HtmlElement) page.getElementById("ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_btnSearch_Table");
page = (HtmlPage) theElement5.click();

is this don't work, you can enable log on debug level to se whats happening, or try this(works on FF):

ScriptResult result = page.executeJavaScript("document.getElementById('ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_btnSearch_Table').onclick()");
final Page newPage = result.getNewPage();

Hint: use Firebug to check the page variables and elements, and page.asXml() to get the real state of the page.

The first one still returns the old page. The second one is throwing an exception and terminating. I added the exception log in the question.

also can it be because the site is actually going to a loading screen first and then displaying the results?

java - How to click on a javascript button in htmlunit - Stack Overflo...

java javascript html htmlunit
Rectangle 27 4

Maybe you need to wait for the javascript to excute and load first. I suggest to use the following command:

webClient.waitForBackgroundJavaScript(1000);
JavaScriptJobManager manager = page.getEnclosingWindow().getJobManager();
while (manager.getJobCount() > 0) {
    Thread.sleep(100);
}
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.setAjaxController(new AjaxController(){
    @Override
    public boolean processSynchron(HtmlPage page, WebRequest request, boolean async)
    {
        return true;
    }
});

java - Clicking on javascript anchor with htmlunit does not seem to wo...

java javascript anchor htmlunit
Rectangle 27 36

You can use a 'temporary' submit button:

WebClient client = new WebClient();
HtmlPage page = client.getPage("http://stackoverflow.com");

// create a submit button - it doesn't work with 'input'
HtmlElement button = page.createElement("button");
button.setAttribute("type", "submit");

// append the button to the form
HtmlElement form = ...;
form.appendChild(button);

// submit the form
page = button.click();

You my friend are a genius! Been working around this for a week now!

It is the solution , I used it a lot of tile in the past and never had any problem.

java - HtmlUnit, how to post form without clicking submit button? - St...

java htmlunit
Rectangle 27 2

I took a look at the page and was able to cast a vote using a slightly different method. I prefer to use Selenium (http://www.seleniumhq.org/download/). I was able to use Selenium in Java to successfully cast a vote using the very crude code below. You can edit and optimize this code to your specific needs. I watched the whole process in an Internet Explorer driver but you could also use PhantomJS (http://phantomjs.org/download.html) as your driver if you do not want the window to show. Here is my simple code, the second argument of the setProperty method is the path to your driver executable this will be unique to your computer (you can download the IE driver on the Selenium downloads page as well):

import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;

public class SeleniumTest()
{
    public static void main(String[] args) 
    {
        try
        {
            System.setProperty("webdriver.ie.driver"," IEDriverServer.exe");
            WebDriver driver = new InternetExplorerDriver();
            driver.get("http://www.ultimateprivateservers.com/index.php?a=in&u=IkovPS");
            Thread.sleep(3000); //use the wait as shown below
            WebElement button = driver.findElement(By.linkText("Enter and vote"));
            button.click();
            driver.close();
            driver.quit();
        }catch (InterruptedException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

A better way to wait for the page to load would be like this:

WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Enter and vote")));

You could also find the button using the class like:

WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("btn-danger")));

it works like charm! The help is much appreciated, thanks :)

htmlUnit clicking a link button - Java - Stack Overflow

java htmlunit
Rectangle 27 7

WebRequest requestSettings = new WebRequest(new URL("http://localhost:8080/TestBox"), HttpMethod.POST);

// Then we set the request parameters
requestSettings.setRequestParameters(Collections.singletonList(new NameValuePair(InopticsNfcBoxPage.MESSAGE, Utils.marshalXml(inoptics, "UTF-8"))));

// Finally, we can get the page
HtmlPage page = webClient.getPage(requestSettings);

java - HtmlUnit, how to post form without clicking submit button? - St...

java htmlunit
Rectangle 27 3

Without knowing much about the webpage you're accessing, you just can't perform an AJAX request with JavaScript disabled. If changing that doesn't result in success, then you will have to keep debugging, but make sure JavaScript is enabled.

Additionally, make sure you're using HtmlUnit 1.12 and update all the deprecated methods in your code.

Welcome :) Just check the link in the answer to remove the awful output. (I've just had to update the link as that answer was a bit outdated)

java - HtmlUnit button click - Stack Overflow

java button htmlunit
Rectangle 27 1

The HtmlUnit getInputByValue() method operating on a <form> will only return types of HtmlInput, and the only Input Button type -- HtmlButtonInput -- represents <input type="button"> and NOT <button>. You will need to change your HTML or use a different HtmlUnit method call.

It isn't my webspage, so I can't change the html. Which method should I use? since the only 'unique' item in the submit button is the value

Java HtmlUnit form.getInputByValue("Login Now!").click(); - Stack Over...

java htmlunit
Rectangle 27 2

final HtmlSubmitInput button = form.getInputByName("submitbutton");
final HtmlPage page2 = button.click()
@Test
public void submittingForm() throws Exception {
    final WebClient webClient = new WebClient();

    // Get the first page
    final HtmlPage page1 = webClient.getPage("http://some_url");

    // Get the form that we are dealing with and within that form, 
    // find the submit button and the field that we want to change.
    final HtmlForm form = page1.getFormByName("myform");

    final HtmlSubmitInput button = form.getInputByName("submitbutton");
    final HtmlTextInput textField = form.getInputByName("userid");

    // Change the value of the text field
    textField.setValueAttribute("root");

    // Now submit the form by clicking the button and get back the second page.
    final HtmlPage page2 = button.click();

    webClient.closeAllWindows();
}

The OP was edited and now it says that there is not a submit button.

java - HtmlUnit, how to post form without clicking submit button? - St...

java htmlunit
Rectangle 27 1

HtmlAnchor link = null;
    for (HtmlAnchor anchor : anchors) {
        String str = anchor.asText();
        if (anchor.asText().equals("Start"))
           link = anchor;
    }
    HtmlPage page2 = link.click();

When you get the from, you use //form (Selects nodes in the document from the current node that match the selection no matter where they are). However, when you try to get anchor, you use /form (Selects from the root node). I don't know structure of your entire form, but that might be issue. In the meantime, could you try this: HtmlAnchor hyperElm = (HtmlAnchor)form.getFirstByXPath("//a[@id=lightboxlink]");

javascript - Java HtmlUnit click on anchor - Stack Overflow

java javascript html htmlunit
Rectangle 27 1

have you tried using

webClient.getOptions().setRedirectEnabled(true);

java - HTMLUnit not clicking a button - Stack Overflow

java htmlunit
Rectangle 27 1

What's the newPage.getWebResponse().getContentAsString() content ? I guess your page may contains many html elements with name "allow"

It's best to be sure that you get an unique and right element, there's many ways to do it :

element.getElementById("id")
page.getFirstByXPath("xpathExpr") || page.getByXPath("xpathExpr")

When playing with inputs, it's always a good way to get as variable the form to manipulate the inputs. eg :

HtmlForm form = page.getforms(0); 
form.getInputByName("name");
form.getInputByValue("value");

BTW, some tips : initialize the webclient with FF settings like this : client = new WebClient(BrowserVersion.FIREFOX_3_6); it got the best html code coverage ( http://build.canoo.com/htmlunit/artifacts/ )

client.setJavaScriptEnabled(false);client.setThrowExceptionOnScriptError(false);
HtmlForm form2 = newPage.getFirstByXPath("//form[@action='authorize']");     HtmlPage finalPage =  form2.getButtonByName("allow").click();

java - HTMLUnit Redirection and Button Click - Stack Overflow

java htmlunit
Rectangle 27 1

How about getting use of built-in javascript support? Just fire submit event on that form:

The code supposes you want to submit first form on the site.

And if the submit forwards you to another site, just link the response to the page variable:

HtmlForm form = page.getForms().get(0);
page = (HtmlPage) form.fireEvent(Event.TYPE_SUBMIT).getNewPage();

java - HtmlUnit, how to post form without clicking submit button? - St...

java htmlunit
Rectangle 27 1

page.executeJavaScript("javascript:submitForm(document.getElementById('localLogin'), 'submit');");

Btw, are you sure the url will change after form submission?

java - use Htmlunit, doesn't work button click(); - Stack Overflow

java javascript login htmlunit
Rectangle 27 1

Seems that sending WebRequest directly works. So first I load login page for captcha etc to be loaded, and then:

String formData = String.format(LOGIN=LOGIN&PB=9&isAA=&sso=&initP=gogo&loginId=%s&passwd=%s&checkcode=%s", loginName, password, captcha.toUpperCase());
WebRequest req = new WebRequest(new URL(url), HttpMethod.POST);
req.setRequestBody(formData);
resultPage = (HtmlPage) getWebClient().getPage(req);

And new page is returned. However then that page says that I dont support frames but that is all different issue.

java - HtmlUnit a href click not working - Stack Overflow

java onclick href htmlunit
Rectangle 27 1

Without knowing the url to analyze your page i can only guess.

webClient.getOptions().setJavaScriptEnabled(true);
webClient.waitForBackgroundJavaScript(30000);

is not a option. Doing it during WebClient setup is useless. You have to place this call after your click

loginPage = htmlButton.click();
webClient.waitForBackgroundJavaScript(30000);

Maybe it is an option to use WETATOR (www.wetator.org) for your testing. Interaction with an web page is much simpler and all the wait magic is done automatically for you.

That helps. Thank you but now next problem - When i login to the application in browser, I see submit button goes disabled for a while and then it redirects to next page. In our code, page returned from clicking the button has button as disabled and that's it. Nothing is happening after it. It's not re-directing to the next page.

One more thing i checked in my page, there are many scripts with <script type="text/html" and in htmlunit, this script is being skipped. This method is being called internally - com.gargoylesoftware.htmlunit.html.HtmlScript.isJavaScript(String typeAttribute, final String languageAttribute) and typeAttribute is text/html and languageAttribute is empty when this method is called. As there is no support in the code to execute text/html, i am getting "Script is not JavaScript (type: text/html, language: ). Skipping execution" log statment Any idea how such scripts can be executed?

Can you please switch to the latest HtmlUnit version - we did so many changes and we are not able to support that old versions.

java - htmlunit knockout js not working. Returning same page on clicki...

java knockout.js htmlunit
Rectangle 27 0

page.executeJavaScript("javascript:submitForm(document.getElementById('localLogin'), 'submit');");

Btw, are you sure the url will change after form submission?

java - use Htmlunit, doesn't work button click(); - Stack Overflow

java javascript login htmlunit
Rectangle 27 0

It seems as if this code throws an exception loading the initial page (specifically executing the JavaScript). From my experience this isn't uncommon and can be attributed to HtmlUnit's JavaScript engine(Rhino).

This code should get the source of the page:

package javaapplication1;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Selenium  {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();

        driver.get("http://www.atb.bergamo.it/ITA/Default.aspx?SEZ=2&PAG=38&MOD=LINTRV");
        WebElement filtra = driver.findElement(By.name("ctl13$btnFilter"));
        filtra.click();
        //make sure to wait till the results div is loaded
        //waits 10 seconds or then throws an exception
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                List<WebElement> divTags = d.findElements(By.tagName("div"));
                for(WebElement tag: divTags){
                    if(tag.getAttribute("id").equalsIgnoreCase("ctl13_divRisultati")){
                        return true;
                    }
                }
                return false;
            }
        });
        String pageSource = driver.getPageSource();
        System.out.println(pageSource);
        driver.quit();
    }
}

java - HtmlUnit click event on submit button doesn't work - Stack Over...

java htmlunit