saab613
saab613

Reputation: 213

Webdriver and Popups in HtmlUnitDriver

I'm currently trying to execute a JavaScript pop-up using webdriver's HtmlUnitDriver and its producing a strange outcome.

The same line of code works fine using the FirefoxDriver however once it is switched to HtmlUnitDriver it stops working. The simple code I'm using is here:

Alert alert = driver.switchTo().alert();
alert.accept();

Is HtmlUnitDriver able to handle Java pop-ups, or is this a limiting point of HtmlUnitDriver.

it is a Javascript Confirm pop-up. We have tried to use the firefox properties with HTMLUnitDriver by doing:

driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3); 

This was unsuccessful.

A side question, Would JavaScript need to be enabled for HTMLUnitDriver for us to interact with the Confirm pop-up box? If YES, Does anyone know how we can turn this on?

driver = new HtmlUnitDriver(capabilities);

does not seem to work and is not recognized.

Any help would be greatly appreciated. Cheers

Upvotes: 4

Views: 2203

Answers (1)

Joao
Joao

Reputation: 616

This appears to be a long running issue https://code.google.com/p/selenium/issues/detail?id=1105

I used a JavaScript workaround for the confirm popup. Here is an example in Python:

self.driver = WebDriver(
    command_executor='http://localhost:4444/wd/hub',
    desired_capabilities=DesiredCapabilities.HTMLUNIT)

self.driver.execute_script("window.confirm = function(msg) {return true;};")
self.find_element_by_id('mybutton').click()

Override the confirm() function before you use it and make it return true for Accept. Then you trigger the click event. Change it to false for Cancel.

Pretty neat. I found this somewhere else on StackOverflow but can't remember where.

Upvotes: 4

Related Questions