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(); } }
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:
driver
<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");