Managing notification popup using JavaScript with Selenium WebDriver

Using Selenium WebDriver 2.53.1.1 in Visual Studio 2015 with C#.

My testing application has Alert Pop Ups that I successfully handle using the code snippet below:

 ts.getDriver().SwitchTo().Alert().Accept();

Recently, I encountered a new challenge where some webelements were not being found anymore. To address this issue, I had to resort to running JavaScript to execute the web element as shown in the code snippet below:

    public void SelectHREFCID()
    {
        IJavaScriptExecutor js = ts.getDriver() as IJavaScriptExecutor;
        js.ExecuteScript("arguments[0].click()",hrefCID);
        try
        {
            ts.getDriver().SwitchTo().Alert().Accept();
        }
        catch (NoAlertPresentException)
        {
            // do nothing...
        }// This workaround helps find the hyperlink!
    }

However, after executing the js.ExecuteScript line, an unexpected pop-up message appears, preventing my code from reaching the Alert().Accept() statement which then causes the program to halt at that point. Debugging revealed that I was unable to proceed beyond the js.ExecuteScript line. Any suggestions on how to manage this situation?

UPDATE: Upon executing the js.ExecuteScript command, https://i.sstatic.net/jM1m1.jpg

, the following dialog is displayed

https://i.sstatic.net/1MFnC.jpg

Once this pop up shows, my Selenium Code fails to continue into the try-catch block for handling the Alert

Latest update as of 9/9

I tried both ways of handling the alert https://i.sstatic.net/cnBPG.jpg https://i.sstatic.net/72iHx.jpg

However, my selenium code stops execution when the pop up appears

js.ExecuteScript("arguments[0].click().hrefCID);

**UPDATE 09/12****

I updated my window.alert and this resolved the issue (to confirm)

 IJavaScriptExecutor js = ts.getDriver() as IJavaScriptExecutor;
        js.ExecuteScript("window.confirm = function(){return true;}");
        js.ExecuteScript("arguments[0].click()",hrefCID);
      //  js.ExecuteScript("window.alert = function() { return true;}");

Answer №1

It appears that this alert is causing the code to halt execution. To bypass this, you can run the following script before clicking to override the confirmBox function using JavaScript:

js.ExecuteScript("window.confirm = function() { return true;}")

Once done, proceed with executing the following script to click on the button:

js.ExecuteScript("arguments[0].click()",hrefCID);

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

Vue3 and Ionic combined to create a Component that became a reactive object in Vue

Vue is issuing a warning about receiving a Component as a reactive object, which can cause unnecessary performance overhead. The warning suggests using markRaw or shallowRef instead of ref to avoid this issue. However, in my code, I am not explicitly using ...

Error encountered while integrating sweetalert into Vue.js framework

Can anyone help me with importing sweetalert2 correctly to use Swal in this file only? I tried using <script src = "https://cdn.jsdelivr.net/npm/sweetalert2@9"> but it's not working as expected. What could be the issue? <script ...

Explore a child's list on Selenium to search for a specific text

I am struggling because I am unsure how to effectively search for a specific text (String) within a child element of a list. Here is the structure I am working with: <div class="father_class"> <div class="child"> <div class="Elements" ...

Determine the array with the highest value by comparing multidimensional arrays in JavaScript

I have a multidimensional array with names and corresponding integer values. I want to compare the integer values within each nested array to find and return the one with the highest value. var nums = [ ['alice', 'exam', 60], [ ...

Ways to retrieve a variable within the init() function

My current project involves using datatables along with ajax to display information dynamically. Below is the code snippet I am working with: // Setting up the module var DatatableAdvanced = function() { // Examples of Basic Datatables var _c ...

The utilization of the JavaScript null coalescing operator ?? resulted in a crash of the .NET JS minifier/bundler

I'm facing an issue with my code where using the ?? operator causes a crash in the MVC built-in bundler/minifier. The problematic line is: span.textContent = typeof (info) === 'object' ? info.column.caption ?? info.column.dataField : info; ...

What is the process for installing a mobile application (Android/iOS) in emulators directly through accessing a path from a different network location using appium?

I have android and iOS emulators running on Windows and Mac machines, respectively. The application is generated by the CI system in a network location (path: \xxx.yyy.com\\Mobile\android and \xxx.yyy.com\\Mobile\io ...

How can I retrieve a Jquery tooltip from a HiddenField?

I am trying to utilize a hidden field in my asp.net calendar to display a message using JQ Tooltip. However, when I attempt to use the value of the HiddenField for the tooltip, I encounter an error. $("#hf_taskID_cal").tooltip($("#hf_taskID_cal").val()); ...

Ways to dismiss a browser's native popup when using Jasmine JS

Can anyone help me figure out how to close a native web browser popup in Jasmine JS? I'm struggling with this dialog box that keeps popping up during all my tests. Your assistance is greatly appreciated! Here's the code I'm working with: ...

click event on ion card

Attempting to handle a click event within an ion-card using Ionic 5.0.2 version has presented some challenges. Despite my efforts, I have not been successful in handling the event with the expected function. Here is a snippet of my code: Dynamic card list ...

Guide to capturing jquery form submissions in a Chrome extension

I am facing an issue with my simple content script in a Chrome extension. It successfully catches form submits triggered by a button (see code), but not when called by jQuery. I can't seem to figure out what's wrong. content.js --------- jQuery( ...

LINQ error due to implicit conversion issue

I have a list of all IDs. //Sample Code List<IAddress> AllIDs = new List<IAddress>(); AllIDs= AllIDs.Where(s => s.AddressId.Length >= s.AddressId.IndexOf("_")) .Select(s => s.AddressId.Substring(s.AddressId.IndexOf("_") ...

Sustained concentration on NextJS

As I embark on my journey of building my very first NextJS site, I have encountered a strange anomaly. Within my site's header, I have incorporated several <Link /> components that serve as links to different pages of my site. These links are a ...

Utilize the jQuery function as a callback argument

There is a jQuery plugin that I am currently working on: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head><title></title> <script type="text/javascript" sr ...

Exploring the topic of nested Firestore listeners and effectively managing the process of unsubscribing

I am interested in implementing a nested listener structure similar to the following: snapshotListeners() { firestore() .collectionGroup() .where() .onSnapshot({ error: //Handle error next: firstSnapshot => { first ...

My React application is being loaded by Express regardless of the route I access. What could be causing this issue?

I'm struggling to access the json data located at /species because express always seems to load the react app regardless of the route I use. Can someone help me identify the issue? Here is an excerpt from my server.js file: const app = require(' ...

How to create an editable HTML table cell using the <td> element

When pulling information from a database table and placing it into an HTML table, I encountered an issue with allowing the user to edit the data. I attempted to use jQuery to create a click event for editing, but when utilizing <textarea> or <inpu ...

How to update a MongoDB GridFS file without changing the ID using C#

I need assistance updating a gridfs file without changing the original id. I attempted to delete and upload a new file, but I cannot change the id and prefer not to use the filename as the id if possible. I am working with the 2.10.2 c# driver. Any help ...

Employ a variable in order to send json

Is it possible to insert data into a predefined JSON variable using a variable? Here are the predefined JSON variables: data(){ return { dataMON: [], dataTUE: [], dataWED: [], dataTHU: [], ...

When trying to perform a Many-to-Many insert in ASP.NET Core 2.2, a "NotSupportedException: Collection was of a fixed size" error is returned

I encountered an issue with "NotSupportedException: Collection was of a fixed size" while trying to insert into the linking table for a Many-to-Many relationship in ASP.NET Core 2.2. Interestingly, I was able to manually insert values directly into SQL Ser ...