Selenium testing - Immediate disappearance of OK/Cancel popup

Something strange is happening here. When I manually test this, I click the delete button, wait for the popup to appear https://i.sstatic.net/6PMS4.png and then click OK to remove the record successfully. However, when I attempt the same process in Java/Selenium, the outcome is different-

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 methods result in the OK/Cancel popup appearing briefly before disappearing.

This is the code for the delete 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 userDelete function

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

I have also tested this in Edge and encountered the same issue, ruling out a Chrome-specific problem.

If anyone has any insights into what might be causing this, please let me know.

Further testing reveals the following: I set a breakpoint just before the script clicks the delete button and run the script. The page loads as expected, but then I encounter the issue.

  1. When I manually click the Delete button, the popup appears and remains visible until I interact with it.

  2. When stepping through the code, the popup appears briefly and then disappears.

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

  3. When using this code, the result is the same as in #2.

    _driver.findElement(By.id("ctl00_cpContentLeft_btnDeletePatient")).sendKeys(Keys.SPACE);

  4. Lastly, I attempt a double click, but nothing happens. In all tests, no errors are reported in the console.

Answer №1

Make sure to wait for the page to load fully before clicking the delete button to prevent the sudden disappearance of the pop. Add a delay before deleting to ensure a smooth experience.

Follow these steps:

  1. Wait for the page to load completely (insert delay here)
  2. Click on the delete button
  3. Confirm by clicking the Okay button

For further guidance, include additional code snippets and screenshots for a more comprehensive understanding.

Answer №2

This appears to be a JavaScript alert notification. In Selenium, there is a specific approach for handling alerts that does not require clicking on the OK button immediately. It is recommended to first wait for the alert to appear and then switch to it.

Furthermore, Selenium provides specific methods for managing alerts, such as alert.accept() for clicking on OK and alert.dismiss() for clicking on Cancel.

Here is a snippet from the Selenium documentation demonstrating how to handle alerts:

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());

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

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

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

//Retrieve the alert text and store it in a variable for future use
String text = alert.getText();

Answer №3

Encountering the identical error, I experimented with resizing the browser window, which successfully resolved my issue.

webdriver.ExecuteScript($"document.body.style.zoom='100%'");

Answer №4

Great news - I've located the solution!

The problem stemmed from the chromedriver, but adding this line resolved it:

chromeOptions.setCapability("unexpectedAlertBehaviour", "ignore");

Now the popup will stay active until it is dealt with in the script.

Every day is a chance to learn and grow - thank you for your assistance!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to generate a dynamically interpolated string in JavaScript?

I'm currently developing a reusable UI component and am exploring options to allow the user of this component to provide their own template for a specific section within it. Utilizing TypeScript, I have been experimenting with string interpolation as ...

Assigning a class to an li element once the page has finished loading

I am facing an issue with my navigation bar where the links are dynamically loaded from a database using a foreach loop. Although the nav bar is static, I want to apply an 'Active' class to the link when it is currently active. Despite trying to ...

utilizing parent scope in a jQuery function callback

Currently, I am facing an issue concerning a jQuery callback working on a variable that is outside of its scope. To illustrate this problem, consider the code snippet below: $('#myBtn').on('click', function(e) { var num = 1; / ...

Is there a way to determine if a Dojo dialog has been successfully loaded on the page?

I have a function that needs to close a Dojo dialog if it is currently open. How can I determine if a dojo dialog is active? Should I rely on pure JavaScript and check for its existence by ID? if (dijit.byId("blah") !== undefined) { destroyRecursive ...

Dynamically populating checkboxes and dynamically setting their checked state

I'm working with a for loop that dynamically generates 7 checkboxes in a row. Here's how it looks: @for (int i = 1; k < order.Rows.length; i++) { Row: @i <ul> @for (int j = 1; j < order.NumCheckboxes.length; j++) ...

Developing a react native library (create-react-native-library) incorporating a distinct react-native version within its designated Example directory

I'm looking to develop a React Native library, but the testing folder (example folder) it contains the latest version of React Native. However, I specifically need version 0.72.6 in the example folder. Is there a command for this? Current command: np ...

Ways to activate an event based on the dimensions (width/height) of

Exploring ways to implement an if statement based on specific width/height values using this code example. Check out the code snippet here My approach: <p id="confirmation">Try again!</p> <script> if (new dynamicSize.width() < ...

The constructor error in ng-serve signalizes an issue in

Currently, I am developing an AngularJS application. However, when attempting to start the dev server, I encountered an issue with my ng serve command: https://i.stack.imgur.com/QujSe.png ...

What is the method to display checkboxes using Selenium?

Is there a way to display the Visible Text of all checkboxes on a webpage using Selenium with Java? ...

Extract Network Response Body with Selenium in Python

I have been using Selenium to interact with data received after a GET request from a website. The API accessed by the website is not public, so when I try to retrieve the data using the URL of the request, I receive {"message":"Unauthenticat ...

What is the best way to retrieve the nearest form data with jQuery after a child input has been modified?

I have a page with multiple forms, each containing several input checkboxes. When one of the form inputs changes, I want to gather all the parent form's data into a JSON array so that I can post it elsewhere. I'm having trouble putting the post ...

Unexpected behavior: custom event firing multiple times despite being emitted only once

I am utilizing the ws module for incorporating web sockets functionality. An event named newmessage seems to be triggering multiple times in correlation with the number of active sockets connected to the web-socket-server. The scenario puzzled me initiall ...

Activate animation while scrolling the page

I am using a progress bar with Bootstrap and HTML. Below is the code snippet: $(".progress-bar").each(function () { var progressBar = $(this); progressBar.animate({ width: progressBar.data('width') + '%' }, 1500); }); <body> & ...

How can I efficiently locate identical sequences of cells in two or more arrays?

Unique Example 1 We can explore an interesting scenario by considering two arrays: ('m','o','o','n','s','t','a','r','d') ('s','t','a', ...

Removing a CSS Class Using Tampermonkey: A Step-by-Step Guide

I'm completely new to CSS and javascript, so please bear with me. My goal is to remove the class disable-stream from each of the div elements located under the div with the class "stream-notifications". Below is an image for reference: Even though I ...

Converting Hexadecimal Values to Base32-Encoding Using Javascript

I'm encountering a problem with converting a function from Ruby to Javascript (specifically node.js, but I prefer a solution that is compatible with browsers, if possible). Here is the hex-formatted sha256 digest: "0b08dfe80a49490ae0722b9306ff53c5ab ...

Having trouble capturing the 'notificationclick' event in the service worker when using Firebase messaging with Nuxt.js and Vue.js?

Experiencing difficulties in detecting events other than install, activate, or push in my firebase-messaging-sw.js. Notifications are being received and displayed, but I am unable to detect the event handler for notificationclick. When a firebase notificat ...

What are some solutions for repairing unresponsive buttons on a webpage?

My task is to troubleshoot this webpage as the buttons are not functioning correctly. Here’s a snippet of the source code: <!DOCTYPE html> <html lang="en"> <head> ... </head> <body> <div id="container" ...

I'm looking for ways to incorporate TypeScript definition files (.d.ts) into my AngularJS application without using the reference path. Can anyone provide

I'm interested in leveraging .d.ts files for enhanced intellisense while coding in JavaScript with VScode. Take, for instance, a scenario where I have an Angular JS file called comments.js. Within comments.js, I aim to access the type definitions prov ...

Having trouble accessing VR Path Registry - Selenium Webdriver

While attempting to run a test case using Selenium and Java, I encountered an issue where the page did not fully load, and the following message appeared in my Eclipse console: Unable to read VR Path Registry I am unsure of what this message signifies. ...