Hey there! any question in your mind? Ask It Now!.

Popular Categories

How to handle alert box in selenium web driver?

+2 votes
asked in Software Testing by Testing (190 points)
In my application after save alerts come , are you sure want to change it? like this how i can manage in selenium ?

2 Answers

+2 votes
answered by seleniumGuru Expert (4,570 points)
selected by Testing
Best answer
you can try this it will accepect your alert box.


public void testAlertOk()
    //Now we would click on AlertButton

    try {
        //Now once we hit AlertButton we get the alert
        Alert alert = driver.switchTo().alert();
        //Text displayed on Alert using getText() method of Alert class
        String AlertText = alert.getText();
        //accept() method of Alert Class is used for ok button
        //Verify Alert displayed correct message to user
        assertEquals("this is alert box",AlertText);
    } catch (Exception e) {


+2 votes
answered by raghav (960 points)

You can also see these methods :

Moving Between Windows and Frames

Some web applications have many frames or multiple windows. WebDriver supports moving between named windows using the “switchTo” method:


All calls to driver will now be interpreted as being directed to the particular window. But how do you know the window’s name? Take a look at the javascript or link that opened it:

<a href="somewhere.html" target="windowName">Click here to open a new window</a>

Alternatively, you can pass a “window handle” to the “switchTo().window()” method. Knowing this, it’s possible to iterate over every open window like so:

for (String handle : driver.getWindowHandles()) {


You can also switch from frame to frame (or into iframes):


It’s possible to access subframes by separating the path with a dot, and you can specify the frame by its index too. That is:


commented by Himanshu Expert (5,940 points)
+1 for such a nice explanation.

Related Questions

+1 vote
1 answer 698 views

Not a Member yet?

Ask to Folks Login

My Account
255 Folks are online
0 members and 255 guest online
Your feedback is highly appreciated