barak manos
barak manos

Reputation: 30166

Selenium Web-Driver Firefox Profile - Disable popup and alert windows

I am having a problem with certain websites that cause my browser to prompt an alert when I try to switch to a different URL, or even close the browser. Some examples:

In order to workaround the alert with Selenium, I need to switch to that alert, and then sometimes accept it and sometimes reject it (depending on the contents of the alert).

I wish to avoid solving this problem that way because:

  1. I need to guess whether I should accept the alert or reject the alert.

  2. Switching to the alert sometimes throws an exception, even though the alert is present.

What preferences do I need to set in the Firefox-Profile, in order to prevent the browser from issuing such alerts (or any other alerts for that matter)?

Answers in Java or Python will be highly appreciated.

Thanks

Upvotes: 2

Views: 14689

Answers (4)

K. Dumre
K. Dumre

Reputation: 17

You can't disable the popups (alert), just do alert.accept() means clicking the ok button of alert modal or alert.dismiss() means clicking the cancel or close button.

The worst in this case is that you need to wait a certain time if you are not very sure that alert is going to be present or not.

If the alert is present as a result of event sucess( just like clicing the button, webpage asks for your confirmation ) you dont need to do wait wait.until(ExpectedConditions.alertIsPresent()); you can save the time by going to next step which is switching the drver to alert. i.e

 Alert alert = driver.switchTo().alert();
     if ( alert.getText().contains("Are you sure you want to leave this page?")) {
         alert.accept();
     }
     else if ( alert.getText().contains("Some other text which means you need to dismiss")) {
         alert.dismiss();
     }
     else {
         //something else
     }
}

just to be sure you can use a small waiting time, but yes it depends uponthe network speed to load the web page for the case that alert is present when webpage is loaded.

Upvotes: 0

java_geek
java_geek

Reputation: 61

To my knowledge you can only disable that behaviour globally. There is a preference called dom.disable_beforeunload. You should change its value to true. With Selenium, you can create a new customized Firefox profile:

FirefoxProfile customProfile = new FirefoxProfile();
customProfile.setPreference("dom.disable_beforeunload", true);
FirefoxDriver driver = new FirefoxDriver(customProfile);

Upvotes: 6

aish
aish

Reputation: 3019

I don't think Firefox profile would feature disabling such specific elements, but you can hard-code some lines of static logic that would remain consistent across the test case/project.

Like Click on the main page automatically closes the pop-up frame/alert msg on grooveshark.com

    @Test
  public void testUntitled() throws Exception {
    driver.get(baseUrl + "/#!/genre/Rap/1748"); //complete URL becomes http://grooveshark.com/#!/genre/Rap/1748
    driver.findElement(By.linkText("more…")).click(); // clicks a hyper-link which opens up that frame/pop-up
    driver.findElement(By.id("lightbox-outer")).click(); // clicks outside the opened-up frame, or simply clicks on the main page in background
  }

lightbox-outer is the main page.

Upvotes: 0

Steve Weaver Crawford
Steve Weaver Crawford

Reputation: 1059

As far as I know it's not possible to disable native browser events like alerts, so you'll just have to handle them better.

1) You should be able to use alert.getText() to make an informed decision on whether to accept or dismiss an alert.

:

try { 
     WebDriverWait wait = new WebDriverWait(driver, 2); 
     wait.until(ExpectedConditions.alertIsPresent());
     Alert alert = driver.switchTo().alert();
     if ( alert.getText().contains("Are you sure you want to leave this page?")) {
         alert.accept();
     }
     else if ( alert.getText().contains("Some other text which means you need to dismiss")) {
         alert.dismiss();
     }
     else {
         //something else
     }
}
catch (Exception e) {

}

2) Use a WebDriverWait to avoid race conditions. See above

Upvotes: 1

Related Questions