chromedriver headless alerts

Viewed 10117

I have this issue with selenium webdriver tests with chromedriver. Although I can run tests succesfully when using Chrome browser I can't run the same tests in headless mode.

I cannot handle the Js alerts. Actually when taking a screenshot it seems that the alert won't even pop-up.

Alert screenshot

I have tried several workarounds:

1) driver.window_handles --> No other window seems to be present

2) driver.execute_script("window.confirm = function(){return true;}") --> Nothing changed with that script

3) element = WebDriverWait(driver, 20).until(EC.alert_is_present()) and of course an explicit wait

In browser mode I use a plain:

try:
    print driver.switch_to.alert.text
    driver.switch_to.alert.accept()
except NoAlertPresentException as e: 
    print("no alert")

Anyone else having this issue with alerts in headless mode?

  • chromedriver v.2.30.477691
  • Chrome Version 59.0.3071.115
4 Answers

Just wanted to add this in case someone comes across this issue using NightwatchJS.

The above accepted solution worked for me, but using JS it needed a slight modification from

driver.ExecuteJavaScript("window.confirm = function(){return true;}");

I needed to change it to

// this.api.perform() in my case is the same as browser.perform() 
this.api.perform(function () { this.confirm = function () { return true; }})
Related