Skip to content
Advertisement

How to change my Selenium code to HtmlUnit [closed]

I have the sellinium code below and it working fine. What I want is to convert this code to be in HtmlUnit. I can use the HtmlUnitDriver like WebDriver driver = new HtmlUnitDriver();

I want to use a pure HtmlUnit. Below my sellinium code is a tried and failed HtmlUnit code.

import java.util.logging.Logger; 
import org.eclipse.jetty.util.log.Log; 
import org.eclipse.jetty.util.log.StdErrLog; import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.htmlunit.HtmlUnitDriver; import com.gargoylesoftware.htmlunit.BrowserVersion; 

public class SpribeDemoTesting { 

public static void main(String[] args) { 

System.setProperty("webdriver.chrome.driver","path to chromedriver.exe");  

driver = new ChromeDriver();

driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

WebDriverWait wait = new WebDriverWait(driver,30); 

driver.get("https://www.spribe.co/games/aviator"); 

htmlButton3 = (HtmlButton) htmlPage.getByXPath("//div[@class='modal-content']//button[1]");

Can someone give me an example. //HERE, HOW DO I PASS THE NEWLY OPENED WINDOWS LIKE I WOULD DO THIS IN SELENIUM **Set windows = driver.getWindowHandles();  List windowsH = new ArrayList<>(windows); driver.switchTo().window(windowsH.get(1)); **

Advertisement

Answer

You can ask the WebClient for the List of current windows

webClient.getWebWindows()

and then you can ask one of the Windows to get the containing page.

getEnclosedPage()

Usually you have to cast the page to HtmlPage and then you can proceed with the page.

And BTW – there is a major error in your code

webClient.waitForBackgroundJavaScript(30);

is not an option; this call waits for the scripts to finish at the moment you call this. Or in other words, you have to place this call after some action (e.g. click()) to wait for the js if there was some async js triggered.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement