0

URL: https://www.guru99.com/alert-popup-handling-selenium.html

Test Page: http://demo.guru99.com/test/delete_customer.php

From the test page,

1) Enter numeric number in the customer id field

2) Click on Submit button

3) Click on "OK" in the alert message using

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

4) Close the next alert saying "Customer Successfully Delete!"

step 4: I'm not able to use

driver.switchTo().alert().dismiss(); 

like I previously did.

I need help as to how do you close the consecutive pop up alert?

Edit: I'm working with Java

Code
  • 1
  • 2
  • 1
    Possible duplicate of [How wait for alert box to perform the action in Selenium?](https://stackoverflow.com/questions/23007472/how-wait-for-alert-box-to-perform-the-action-in-selenium) – JeffC Mar 12 '19 at 20:03

3 Answers3

0

If you see the browser is refreshing after submission.

So use delay to make sure alert is loaded:

WebDriverWait(browser, 4).until(EC.alert_is_present())

Then use

 alert = browser.switch_to.alert
 alert.accept()

to accept the alert i.e pressing "ok"

Ram
  • 319
  • 1
  • 2
  • 16
  • This link shows how to add delay to java https://stackoverflow.com/questions/12858972/how-can-i-ask-the-selenium-webdriver-to-wait-for-few-seconds-in-java (or) https://www.toolsqa.com/selenium-webdriver/wait-commands/ (or) https://www.guru99.com/implicit-explicit-waits-selenium.html – Ram Mar 12 '19 at 19:30
0

You need to wait for sometime and check the alert is present or not? Because the page is getting loaded once you have accepted the first alert.

Below is the working Java code :

// Launching the browser and Navigating to the given URL
driver.get("http://demo.guru99.com/test/delete_customer.php");

// Locating the Customer ID field and Sending the text
WebElement customerID = driver.findElement(By.name("cusid"));
customerID.sendKeys("Something");

// Locating the Submit and Clicking on it
WebElement submit = driver.findElement(By.name("submit"));
submit.click();

// Switching to the first alert and Printing the alert text and Accepting the alert
Alert alert = driver.switchTo().alert();
System.out.println("=> The alert text is : "+alert.getText());
alert.accept();

// Using the WebDriverWait to wait until the alert is displayed
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.alertIsPresent());

// Once the alert is displayed, switching to that alert and Dismissing it
alert = driver.switchTo().alert();
System.out.println("=> An another alert text is : "+alert.getText());
alert.dismiss();

I hope it helps...

Ali
  • 1,689
  • 1
  • 5
  • 12
0

Not sure if this is the case, but I just hit a similar problem and I'm posting a solution, as your question pops up in Google search results. If the alerts you want to test appear immediately, one after another, like this:

alert('foo');
alert('bar');

... and you have control over server-side JavaScript code, you should separate them, for example like this:

alert('foo');
setTimeout(function(){alert('bar');}, 500);

This way WebDriver will be able to differentiate between those alert boxes.

dotz
  • 884
  • 1
  • 8
  • 17