Search code examples
javajavascripthtmlunit

HtmlUnit prevent javascript verifier from redirecting


I am writing a Java program which automatically plugs information into a website, which then performs some javascript functions on that information.

My problem is that, when the page loads, it automatically invokes some javascript that checks my browser information, and then recommends that I download Java. Naturally I already ave java installed, or else the program wouldn't run!

This is the site that the script redirects me to, in order to perform the check: http://java.com/js/deployJava.txt

the relevant portions of my code:

WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
HtmlPage page = webClient.getPage("https://www.blahblahblah.com");
// this is where I get redirected

HtmlTextInput txtElement = (HtmlTextInput) page.getElementById("itemID");
// this returns null, because I am on a different page than what I expect
txtElement.type("my search info");

page = txtElement('\n');  // to submit the field (there is no submit button)

I have tried all different browser settings, to no effect.

When I go to the same URL in my browser, the page loads without a hitch.

When I tried disabling redirects, getPage returned an empty TextPage, rather than an HtmlPage.

Has anyone else encountered a similar problem, or have any insight? Is there a way to prevent that piece of javascript from executing, but not the rest?

Cross Posted: http://www.experts-exchange.com/Programming/Languages/Java/Q_27765581.html

Thanks in advance,

Asura

UPDATE: As it turns out, I am able to get the information I need through post requests. Thanks for all your help!


Solution

  • You could use JWebUnit wrapped with HtmlUnit and then do something like this:

    public void testFoo() {
      beginAt("blabla");
      ...
      setScriptingEnabled(false);
      ...//Problematic code
      setScriptingEnabled(true);
      ...
    }
    

    Which will temporarily disable JavaScript