Search code examples
htmlhtmlunitselenium-webdriver

The html page not showing correct link when using htmlunit driver


I have written a selenium script which runs in java :

    String pageName = "my test url which invloves an link to html which has javscript excution";
    logger.log(Level.INFO,"Page name : " + pageName);
    WebDriver driver = new HtmlUnitDriver(true);

    logger.log(Level.INFO,"driver instance created " );
    String str ="";

    logger.log(Level.INFO,"opening the url now.... " );
    driver.get(pageName);
    logger.log(Level.INFO,"url is now opened :: url = "+driver.getCurrentUrl());
    logger.log(Level.INFO,"driver now going to sleep = "+driver.getCurrentUrl());
    Thread.sleep(150000);

    logger.log(Level.INFO,"Wake up from sleep now....");
    logger.log(Level.INFO,"URL ::"+driver.getCurrentUrl());
    logger.log(Level.INFO,"PageSource ::"+driver.getPageSource());
    try {
        logger.log(Level.INFO,"Driver going to wait now...");
        driver.wait(100000);
        logger.log(Level.INFO,"Driver came out of wait now normally...");
    } catch (Exception e) {
        logger.log(Level.INFO,"Driver came out of wait now exception::"+e);
    }

    logger.log(Level.INFO,"driver instance task completed " );
    logger.log(Level.INFO,driver.getCurrentUrl());

    logger.log(Level.INFO,driver.getCurrentUrl());
    logger.log(Level.INFO,str);

    driver.close();

Now when I debug this code I get my proper result but when I run this piece of code from servlet I get only the html content not the proper content which is excepted to come from the given link.

I also tried using the firefox driver and the same code works fine with it. I also tried the same code with Web client but the same problem is coming.

Can anyone help me with this exception?

Thanks,


Solution

  • When you debug the code, you can halt at checkpoints. But you cannot stop browser from loading. It loads itself independently. Htmlunitdriver pagesource returns all html contents as it cannot execute javascript completely.If the htmlpage contains Ajax, javascript calls then it shows the calls as it is .