Silent Chronicle Alerts

I have this problem with selenium webdriver tests with chromedriver. Although I can run tests successfully using the Chrome browser, I cannot run the same tests in headless mode.

I cannot handle Js warnings. In fact, when taking a picture, it seems that the warning will not even appear.

Warning screenshot

I tried several workarounds:

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

2) driver.execute_script("window.confirm = function(){return true;}") → Nothing has changed with this script

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

In browser mode, I use plain:

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

Does anyone else have this problem with headless alerts?

  • chromedriver v.2.30.477691
  • Chrome Version 59.0.3071.115
+8
selenium-webdriver selenium-chromedriver google-chrome-headless
source share
3 answers

There is still a problem with Chrome 61, so I spent some time looking for another solution. My favorite, because of this simplicity, introduces javascript before the warning is displayed in order to automatically accept the warning.

Just put the following line of code before the line that triggers the warning:

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

Works with both headless chrome and PhantomJS.

+11
source share

It seems like I have the same problem when working without chrome chrome. A warning window does not pop up on screenshots. It works great on chrome, but not headless chrome.

I am working on chrome 60.0.3112.72 and chrome driver 2.30

Because headless chrome automatically discards warnings. Check this out: https://bugs.chromium.org/p/chromium/issues/detail?id=718235


By the way, how can you take a picture in chrome 59 in headless mode? chrome 59 has an error that takes every 1x1 pixel screen shot in headless mode, so I upgraded to chrome 60.

+3
source share

Since Chrome headless does not (currently) support alerts, you must secure the alert() and confirm() methods. This is the approach I used (in C #):

  /// <summary> /// The Chrome Headless driver doesn't support alerts, so we need to override the window.alert method to get the expected behavior. /// </summary> /// <param name="driver">The active IWebDriver instance</param> /// <param name="result">The result that the alert should return, ie, true if we want it "accepted", false if we don't</param> public static void SetupAlert(this IWebDriver driver, bool result) { // ks 7/27/17 - The Chrome Headless driver doesn't support alerts, so override the various window.alert methods to just set const string scriptTemplate = @" window.alertHandlerCalled = false; window.alertMessage = null; window.alert = window.confirm = function(str) { window.alertHandlerCalled = true; window.alertMessage = str; return {{result}}; };"; var script = scriptTemplate.Replace("{{result}}", result.ToString().ToLower()); var js = (IJavaScriptExecutor)driver; js.ExecuteScript(script); } /// <summary> /// This is an optional accompaniment to the <see cref="SetupAlert"/> method, which checks to see /// if the alert was, in fact, called. If you don't want to bother to check, don't worry about calling it. /// Note that this doesn't reset anything, so you need to call <see cref="SetupAlert"/> each time before calling /// this method. /// </summary> public static void WaitForAlert(this IWebDriver driver, TimeSpan? timeout = null) { const string script = @"return window.alertHandlerCalled"; var js = (IJavaScriptExecutor)driver; var timeToBail = DateTime.Now.Add(timeout ?? TimeSpan.FromMilliseconds(500)); while (DateTime.Now < timeToBail) { var result = (bool)js.ExecuteScript(script); if (result) return; Thread.Sleep(100); } throw new InvalidOperationException("The alert was not called."); } 

I use it as follows:

  Driver.SetupAlert(true); this.ClickElement(ResetButton); Driver.WaitForAlert(); 
0
source share

All Articles