Automating Alert Handling in Selenium WebDriver: Handling Multiple PopUps with Ease

Web Address:

Test Page URL:

On the test page, follow these steps:

1) Input a numerical value in the field labeled as "customer id"

2) Click on the button labelled as "Submit"

3) After seeing an alert message, press "OK" using the following code snippet:

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

4) Proceed to close the subsequent alert that reads "Customer Successfully Delete!"

In carrying out step 4, I encountered difficulties with the implementation of:

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

which worked previously. Any guidance on how to address this issue would be greatly appreciated.

Note: The programming language being used is Java.

Answer №1

In case the browser refreshes after submission,

It is advisable to introduce a delay to ensure that the alert loads:

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

Afterwards, proceed with the following steps:

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

This will successfully accept the alert by clicking on "ok".

Answer №2

Make sure to wait for a bit and verify if the alert is showing up or not. The page loads once you accept the initial alert.

Check out this effective Java script :

// Open the browser and go to the specified URL
driver.get("http://demo.guru99.com/test/delete_customer.php");

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

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

// Switch to the first alert, display the alert text and accept it
Alert alert = driver.switchTo().alert();
System.out.println("=> Alert message: "+alert.getText());
alert.accept();

// Use WebDriverWait to wait until the alert appears
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.alertIsPresent());

// Once the alert shows up, switch to it and dismiss it
alert = driver.switchTo().alert();
System.out.println("=> Another alert message: "+alert.getText());
alert.dismiss();

This code should be helpful...

Answer №3

Could this be the solution to your problem? I recently encountered a similar issue and found a workaround that might help. If you are dealing with alerts that appear in quick succession, like the example below:

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

...and if you have the ability to modify the server-side JavaScript code, consider separating the alerts like this:

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

By doing so, WebDriver will be able to distinguish between the different alert boxes.

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

Guide on how to retrieve a value from a function in React Native

This method retrieves the token data from the API I am utilizing. // api.js var token=""; const getToken = { getTokenData: function(){ fetch("myurl/api/token", { method: "POST", headers: { Accept: "application/json", ...

When the "next" button is clicked on Datatables, the next set of 10 items

I am currently working on creating a table with pagination using datatables. The data is being fetched via ajax, which returns 10 data entries at a time. However, I am facing an issue where the first call only fetches the initial 10 data from the ajax call ...

Is the extends and implements keyword used internally by an anonymous inner class in Java?

As someone who is new to Java, I recently encountered the concept of anonymous inner classes. From what I have gathered so far (please correct me if I am mistaken), anonymous classes act as subclasses of a class or implement an interface while also creatin ...

Struggling to understand the reason behind the malfunction of my promise array

I am currently tackling a project that involves running a series of promises and then pushing their results to an array within the .then() portion. However, I am encountering an issue where even though the promises are providing result values, nothing is b ...

InvalidType: entity does not function as a function

I'm not sure where this issue is originating from, as version 4.0 of CO and version 0.11.14 of Node are installed. The code itself is very simple and worked fine with version 3.1 (just realized the earlier version of co was different) "use strict" co ...

Issue with incorrect date being retrieved in MongoDB response from a node server

My date in mongodb is structured as follows: ISODate("2020-08-03T00:00:00.000+05:30"), However, after querying the date, it appears like this: 2020-08-02T18:30:00.000Z I am looking to get the correct date of 2020-08-03. What could I be doing wr ...

Customize the <td> column based on values and rows. Modify the current jQuery code

This code snippet contains an HTML table that dynamically populates based on the dropdown selection. The script included in the code highlights the best and worst values in the table by changing their background color to green and red, respectively. & ...

Problematic Angular 6 Lazy Loading Situation

Below is the code snippet I am using for lazy loading: const routes: Routes = [ { path: '', redirectTo: '/home', pathMatch: 'full' }, { path: 'home', component: HomeComponent }, { path: 'manager', lo ...

Converting an HTML ul-li structure into a JavaScript object: Steps to save the structure

My HTML structure uses ul and li elements as shown below: <ul class="treeview" id="productTree"> <li class="collapsable lastCollapsable"> <div class="hitarea collapsable-hitarea lastCollapsable-hitarea"></div> <span ...

What methods can be used by the client-side to determine whether a file has been successfully downloaded or received from

When a client-side file download request is initiated, I dynamically create a form element with hidden attributes and submit it to the server via POST. During this process, I need to display a loading spinner that will be hidden once the download is comple ...

AngularJS Service failing to appear on screen

I am trying to access my Github information using Github's API. I can see the correct information when I log my http request, but for some reason it is not showing up on the page. There are no errors being thrown, but the requested data is not display ...

Accessing an object within another object using Typescript

My goal is to access the "rename_fields" object within the main_object collection in order to utilize its field values: export interface StdMap<T = string> { [key: string]: T; } export type StdFileBasedPluginHandlerConfiguration< SourceTy ...

Dispatch an Ajax message with a specified delay in milliseconds before executing the next action

Hey there, I've been attempting to incorporate a delay() or setTimeOut function into this simple send message script, but I seem to be struggling with the syntax. Whenever I try to add these functions, I encounter numerous syntax errors. Even after fi ...

Upgrade an existing create-react-app project ejected with react-app-rewired from webpack 4 to webpack 5

I am currently working on updating an old React application that was initially created using create-react-app and later ejected with react-app-rewired. The task at hand is to upgrade all deprecated dependencies. So far, I have successfully updated react-sc ...

The tilesloaded event in google.maps.event.addListener is unexpectedly failing to trigger

Today, while testing popover messages on my cordova/ionic app, I compiled the app and noticed that the maps weren't loading. Instead, only my custom "Loading map..." spinning icon kept showing. Upon investigating further, I found that var setMap = n ...

Closing md-tooltip automatically after a specified timeout period

I've set up md-chips in Angular material with the following configuration: <md-chips md-chips-disable-input ng-model="session.participants"> <!-- Chip removal button template --> <button md-chip-remove class ...

Python - Automating the process of launching multiple tabs with Selenium

Using Python, I am attempting to open two tabs on Chrome, each directed to a different website. Currently, my code looks like this: from selenium import webdriver from selenium.webdriver.common.keys import Keys import time browser=webdriver.Chrome() bro ...

Ways to dynamically retrieve a key value pair in JavaScript and React

I am currently working with a spreadsheet element where the cell values are stored in an object structure like this: localCells = {A1: {input: 'hi', value: 'world'}, A2: {input:'how', value:'you?'}} The object is q ...

Fade-in with jQuery Javascript when clicked and fade-out once loading is complete

$('.showloader').click(function () { $('.loader').fadeIn(); }); I have a loading css called .loader It is default Display:none I want the .loader to be displayed when the .showloader is clicked. This will submit a registrati ...

Is there a jade plug-in available that enables manipulation using syntax similar to jQuery?

If I have a list of li elements and I want to use JavaScript to find an element with class X and modify some of its attributes, I know it can be done with regular JavaScript, but I'm unsure of how to select existing elements and manipulate them. Is th ...