Alerts¶
Using the methods below you will be able to handle the three types of native popup dialogs from JavaScript:
- Alerts
- Confirmations
- Prompts
Retrieving a Dialog¶
You can retrieve a Dialog to interact with it and fetch information about it such as the message content.
# Retrieve the dialog. If no dialog is found, the dialog will receive None.
dialog = bot.get_js_dialog()
# Get the message content of the dialog.
print(dialog.text)
# Close the dialog.
dialog.dismiss()
# Accept the dialog.
dialog.accept()
# Reply to the dialog.
dialog.send_keys("Hello Dialog!")
import org.openqa.selenium.Alert;
// Retrieve the dialog. If no dialog is found, the dialog will receive None.
Alert dialog = getJsDialog();
// Get the message content of the dialog.
System.out.println(dialog.getText());
// Close the dialog.
dialog.dismiss();
// Accept the dialog.
dialog.accept();
// Reply to the dialog.
dialog.sendKeys("Hello Dialog!");
Handling Dialogs¶
As we have seen above, you can retrieve a Dialog to interact with it and fetch information about it such as the message content.
We also offer a shortcut to handle the dialogs:
# This will send the message "Hello Dialog" and accept the dialog.
bot.handle_js_dialog(accept=True, send_keys="Hello Dialog!")
# This will dismiss the dialog.
bot.handle_js_dialog(accept=False)
// This will send the message "Hello Dialog" and accept the dialog.
handleJsDialog(true, "Hello Dialog!");
// This will dismiss the dialog.
handleJsDialog(false);