Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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-

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading