Dealing with alerts containing JavaScript within a button: Tips and Tricks

I am currently working on automating a SharePoint website using WebDriver and Java. The website has JavaScript embedded in the button code.

Here is the HTML snippet for the button:

<input id="ctl00_ContentPlaceHolder1_btnDelete" class="btn" type="submit" onclick="javascript:return confirm('Please select OK to proceed else Cancel.');" value="Delete" name="ctl00$ContentPlaceHolder1$btnDelete"/>

Below is the code I have written to handle this:

String js = "if (window.alert.myAlertText == undefined) {window.alert.myAlertText = null;  window.alert = function(msg){ window.alert.myAlertText = msg; };}";
//Click delete button 
((JavascriptExecutor) driver).executeScript("arguments[0].click();", driver.findElement(By.xpath(".//*@id='ctl00_ContentPlaceHolder1_btnDelete']")));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", driver.switchTo().alert(), js);

However, I am facing difficulties in accepting or dismissing the alert.

Please provide assistance!

Snapshot of the button :

Snapshot Alert

Answer №1

If you need to switch to an alert, you can do so with the following code snippet and then proceed to accept it:

Alert popup = driver.switchTo().alert();
popup.accept();

Alternatively, if you want to dismiss the alert, use the code below:

Alert popup = driver.switchTo().alert();
popup.dismiss();

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

Problem encountered when trying to use the sharp package in NuxtJS

I attempted to implement this code in my Nuxt project, but it encountered an issue during compilation. Within my plugin/sharp.js file: import vue from "vue" import sharp from "sharp" vue.use(sharp) And in my nuxt.config.js file: plugi ...

Using Jquery to search for an element within a variable and update the text

Having trouble with the following expression: (not even with .text()) $(file.previewElement).find('[data-dz-name]').html(file.name); The variable File.previewElement is defined as: $(".preview-template").html(); The debugger output for $(file. ...

Is there a way to modify the style within a TS-File?

I've created a service to define different colors and now I want to set separate backgrounds for my columns. However, using the <th> tag doesn't work because both columns immediately get the same color. Here's my code: color-variatio ...

What is the best way to align a label, input box, and a small search image on the same line

I'm currently working on developing a mobile page using jQuery Mobile version 1.4.4. I've managed to get the label and input box to display correctly in one line, but I'm encountering an issue with the small search image appearing on the nex ...

What is the best way to extract data from a textarea HTML tag and store it in an array before iterating through it?

I have a project in progress where I am developing a webpage that will collect links from users and open each link in a new tab. I am using the <textarea> tag in HTML along with a submit button for link collection. Users are instructed to input only ...

Looking to fetch the date within the current week using AngularJS between Monday and Sunday?

How can I retrieve the dates for this week from Monday to Sunday? For example, the dates for this week are: Monday = 23/11/2015 to Sunday 29/11/2015. I am looking to obtain the dates for every week, spanning from Monday to Sunday. Technologies: AngularJS ...

"Exploring the world of mocking module functions in Jest

I have been working on making assertions with jest mocked functions, and here is the code I am using: const mockSaveProduct = jest.fn((product) => { //some logic return }); jest.mock('./db', () => ({ saveProduct: mockSaveProduct })); ...

Analyzing database entries containing unique identifiers and formatted with Bootstrap styles

Struggling with properly applying quotation marks around IDs and Bootstrap formatting. The HTML snippet should resemble this: <tr> <th scope="row">1</th> <td>HOU</td><td>-9.8</td> <td id="odds-1">2</td&g ...

Tips for replacing the new line character within a string to allow the <p> tag to display the new line

My current project involves a React application with a specific condition: When a string contains a new line character, the <p>{string}</p> tag that displays the string should be replaced with an HTML new line character. However, simply repl ...

The Jsoup after function fails to meet expectations

I am facing an issue with a method I have, where the property description is of type "Element" and I am attempting to add another element using the Jsoup.after method. Unfortunately, this results in an error message: Exception in thread "main" java.lang. ...

Preventing Page Scroll While New Data is Loading

I am currently working on a React class component that uses Post components. Within this component, there is a button that triggers the loading of more data from the database. The issue I am encountering is that when new data is fetched, the page automatic ...

Unable to determine the reason why the JavaScript plugin is not functioning as expected

I have been attempting to set up a JS modal window and have essentially copied the code for the plugin exactly as instructed, but it is not functioning properly. Here is my code... <!doctype html> <html> <head> <meta charset="utf- ...

What is the process for using npm in conjunction with index.js and index.html?

Is it feasible to launch an application using npm without incorporating express? I have a simple setup with just one script file and one html file. Many resources mention adding node.js alongside express, but I am looking for an alternative method. My des ...

Retrieve the Most Recent Matching Date within an Array

In my mongoDB database, I am searching for datasets with expired date values. When I say expired, I mean that the timestamp of the last element in an array is older than a certain interval from the current timestamp (determined by a category). Each datase ...

Prevent the propagation of a particular CSS class's inheritance

I need to make modifications to an existing HTML5 app that features two unique themes. Here's an example of the current structure: <body class="theme1 theme2"> <div id="div1">I'm satisfied with both themes</div> <di ...

Having difficulties with the 'client request' function in the latest version of Next.js (13.5.1) - still receiving server-side responses

In the current version of Next.js (13.5.3), I am utilizing 'use client' but the component is still rendering from the server-side. Additionally, I am finding it challenging to integrate Tailwind and Ant Design (antd) together in Next.js. If anyo ...

Learn how to extract a specific field from a String containing JSON data using Gson or another Java library

I have a String that contains JSON formatted information, specifically an OAuth token. How can I extract the refresh_token? This is the JSON data printed from the inputLine: { { "access_token":"ffa6ea48612b19966383fd6b134fe2c4daac80ce31a8632284b ...

Downloading files in headless mode using Python and Selenium is not working

I am encountering an issue while trying to download this file: Residential Product Guide & Rate Sheets Residential Rate Sheet -> Click on the download button next to it When I download without using headless mode, everything works perfectly fine ...

Optimal methods for organizing various perspectives on a one-page website

I am developing an application that incorporates AngularJS and Bootstrap. The current setup involves organizing various views using ng-show, allowing for view changes based on button interactions and the enablement/disabling of ng-show values. Within a si ...

The slideToggle function in jQuery seems to have trouble working on a Joomla platform, however, it functions perfectly on jsbin

I'm currently working on a website and I'm trying to implement the jQuery slideToggle feature to move some blocks. Everything seems to be working fine when I test the code on jsbin.com, but for some reason, it's not functioning properly on m ...