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

Popular Categories





How to handle alert box in selenium web driver?

+2 votes
822 views
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.

 

@Test
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
        alert.accept();
        //Verify Alert displayed correct message to user
        assertEquals("this is alert box",AlertText);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

 

+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:

driver.switchTo().window("windowName");

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()) {
    driver.switchTo().window(handle);
}

 

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

driver.switchTo().frame("frameName");

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:

driver.switchTo().frame("frameName.0.child");

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

Related Questions

+2 votes
1 answer 1,270 views
+1 vote
1 answer 547 views
+2 votes
1 answer 135 views
+10 votes
2 answers 9,697 views
+1 vote
1 answer 2,250 views
+5 votes
7 answers 14,605 views
+1 vote
0 answers 298 views

Not a Member yet?

Ask to Folks Login

My Account
755 Folks are online
59 members and 696 guest online
Your feedback is highly appreciated