Wednesday 24 May 2017

Closing an alert with Selenium WebDriverJs

I've been struggling trying to close an alert the browser opens only with a specific patterns I don't control, so I need a piece of code that detects and closes the alert to be able to continue navigating the web and not stopping the execution.

I tried several ways of dealing with the alert, but none of them has been successful for the moment:

Here are some of the examples I've been working with:

1:

driver.wait(driver.switchTo().activeElement().then(
  function() {
    console.log("alert detected");
    driver.switchTo().alert().accept();
  },
  function() {
    console.log("no alert detected");
  }
), 1000);

2:

  driver.wait(driver.switchTo().alert().accept()
        .then(null, function(err)
        {
            console.log("ERROR ALERT");
            console.log(err);
            console.log("ERROR NAME");
            console.log(err.name);
        }), 1000);

3: Inject this piece of code trying to overwrite the alert of the browser to make it to not appear:

  driver.executeScript('alert = function(){};');

Basically these three are the main workarounds I've been struggling with, but for the moment none seems to work properly.



via avilac

No comments:

Post a Comment