- Before clicking on Ok button, take the text message which is available in the alert message and print.
To handle the alert, we need to navigate from main window to alert. Then we can perform actions like either OK or Cancel.
Accept: It is to click OK on the alert message.
Dismiss: It is to click Cancel on the confirmation alert message.
driver.switchto().alert().accept(); // to click on OK button.
driver.switchto().alert().dismiss(); // to click on Cancel button.
Get Text: It is to take the text or label from the application where the webdriver is having the focus.
- The written type of get text is String.
system.out.println(str); // to print the text message