javawindowsseleniumhtmlunithtmlunit-driver

How to switch window on HtmlUnit


I have a selenium code below which switch window to another window. How do I do the same thing with HtmlUnit not HtmlUnitDriver.

Set windows = driver.getWindowHandles();  List windowsH = new ArrayList<>(windows); driver.switchTo().window(windowsH.get(1));

Can someone give me a sample code. Thank you.


Solution

  • Based on HtmlUnit (2.56.0) api docs:

    WebClient webClient = ...
    List<WebWindow> windows = webClient.getWebWindows();
    webClient.setCurrentWindow(windows.get(1));
    

    Maven dependency

    <dependency>
        <groupId>net.sourceforge.htmlunit</groupId>
        <artifactId>htmlunit</artifactId>
        <version>2.56.0</version>
    </dependency>
    

    Reference

    https://htmlunit.sourceforge.io/apidocs/index.html

    https://htmlunit.sourceforge.io/apidocs/com/gargoylesoftware/htmlunit/WebClient.html#getWebWindows--

    https://htmlunit.sourceforge.io/apidocs/com/gargoylesoftware/htmlunit/WebClient.html#setCurrentWindow-com.gargoylesoftware.htmlunit.WebWindow-