Home > other >  Selenium test - OK/Cancel popup disappearing immediately
Selenium test - OK/Cancel popup disappearing immediately

Time:10-26

Odd one here. When testing this manually I click the delete button, wait for the popup enter image description here Then I click OK and the record is removed as expected. But when I try to do the same in Java/Selenium it goes like this->

WebElement element = _driver.findElement(By.id("btnDeletePatient"));
JavascriptExecutor executor = (JavascriptExecutor)_driver;
executor.executeScript("arguments[0].click();", element);

or

_driver.findElement(By.id("btnDeletePatient")).click();

Both have the same response, the OK/Cancel popup will appear and then immediately vanish.

This is the code for the button

<input type="submit" name="ctl00$cpContentLeft$btnPatient" 
value="Delete User" onclick="return userDelete();" 
id="ctl00_cpContentLeft_btnPatient" 
tabindex="81" class="btn btn-outline-primary btn-sm mr-3">

And this is the code for the function userDelete

 function userDelete() {
        if (confirm("Are you sure you wish to delete this user?")) {
            return true;
        }
        else {
            return false;
        }
    }

Also I have now tried the same in Edge, same thing so this does not appear to be a Chrome issue.

Anyone got any ideas what is causing this please?

CodePudding user response:

I hope you are clicking the delete button before page loads completely, that is why pop is disappearing suddenly. please add wait before clicking delete button.

Steps:

  1. Load the page completely (add wait here)
  2. Click the delete button
  3. click Okay button.

please add some more codes here and screenshots too. that would be more helpful

CodePudding user response:

This looks like a java script alert. In selenium, we have specific way to handle alert. You do not need to click on ok. You can first wait for alert to be present. If it is present, then switch to that alert.

Then, We have specific methods to handle alerts in selenium.

alert.accept() clicks on ok.

alert.dismiss() clicks on cancel.

Sample code from selenium documentation:

driver.findElement(By.linkText("See a sample prompt")).click();

//Wait for the alert to be displayed and store it in a variable
Alert alert = wait.until(ExpectedConditions.alertIsPresent());

//Type your message
alert.sendKeys("Selenium");

//Press the OK button
alert.accept();

//Press the Cancel button
alert.dismiss();

//Store the alert in a variable for reuse
String text = alert.getText();
  • Related