How to Handle Alerts and Dialogs in Vibium
Handle alerts, confirms, and prompts in Vibium with on_dialog() and dialog.accept(). Runnable Python examples for accepting, dismissing, and reading dialogs.
To handle alerts and dialogs in Vibium, register a handler with vibe.on_dialog(handler) before the action that opens the dialog, then call dialog.accept() or dialog.dismiss() inside it. Native browser dialogs — alert(), confirm(), prompt(), and beforeunload — block the page until they are answered, so Vibium surfaces each one as a dialog object passed to your callback. Inside the handler you can read dialog.message() for the text and dialog.type() for the kind of dialog, then respond: dialog.accept() clicks OK (and, for a prompt, you can pass the text to return), while dialog.dismiss() clicks Cancel. Vibium maps these to the WebDriver BiDi browsingContext.userPromptOpened event and handleUserPrompt command under the hood, so behavior is consistent and standards-based. Register the handler first, then trigger the dialog — the callback fires the moment the prompt appears.
How do I accept a JavaScript alert in Vibium?
Register a dialog handler with on_dialog() before you trigger the alert, then accept it inside the callback. The handler must be in place first, because the dialog blocks the page as soon as it opens.
from vibium import browser_sync as browser
vibe = browser.launch()
vibe.go("https://example.com")
# Register the handler BEFORE the action that opens the dialog.
vibe.on_dialog(lambda dialog: dialog.accept())
# This click triggers a window.alert(...) — the handler accepts it.
vibe.find(role="button", text="Show alert").click()
vibe.quit()The lambda dialog: dialog.accept() callback runs automatically when the alert appears, clicking OK so the page unblocks and your script continues. Because the handler is registered up front, you never have to poll for the dialog or guess at timing — Vibium invokes the callback as soon as the prompt opens.
How do I read a dialog's message and type?
Inside the handler, call dialog.message() to read the text and dialog.type() to learn whether it's an alert, confirm, prompt, or beforeunload. This lets you assert on the wording or branch your response.
from vibium import browser_sync as browser
vibe = browser.launch()
vibe.go("https://example.com")
def handle(dialog):
print("type:", dialog.type()) # "confirm"
print("message:", dialog.message()) # "Delete this item?"
dialog.accept()
vibe.on_dialog(handle)
vibe.find(role="button", text="Delete").click()
vibe.quit()A named function (instead of a lambda) is the natural fit once you want to inspect the dialog before responding. You can save dialog.message() to a variable and assert on it, which is handy when a test needs to verify the exact confirmation copy a user would see.
How do I dismiss a confirm or cancel a prompt?
Call dialog.dismiss() to click Cancel. For a confirm() this makes the page see a "false" result; for a prompt() it cancels input entirely so the prompt returns null.
from vibium import browser_sync as browser
vibe = browser.launch()
vibe.go("https://example.com")
# Dismiss every dialog — useful for declining a "Leave page?" beforeunload.
vibe.on_dialog(lambda dialog: dialog.dismiss())
vibe.find(role="button", text="Cancel changes").click()
vibe.quit()Dismissing is the right move when you want to test the "negative" branch — for example, confirming that clicking Cancel on a delete confirmation leaves the row untouched. Pair this with a find() check afterward to assert nothing changed.
How do I answer a prompt dialog with text?
Pass the answer string to accept(). For a prompt() dialog, that string becomes the value JavaScript receives; for alerts and confirms the argument is simply ignored.
from vibium import browser_sync as browser
vibe = browser.launch()
vibe.go("https://example.com")
# prompt("What's your name?") will receive "Vibium"
vibe.on_dialog(lambda dialog: dialog.accept("Vibium"))
vibe.find(role="button", text="Enter name").click()
vibe.quit()This is the only way to supply input to a native prompt, since the dialog is rendered by the browser chrome, not the page DOM — you can't find() and type() into it. Use dialog.accept("text") for prompts and plain dialog.accept() for alerts and confirms.
Why dialogs need a handler, not a find()
Native dialogs live in the browser's UI layer, outside the page's HTML, so locator strategies don't reach them. Vibium routes them through the BiDi userPromptOpened event instead, calling your on_dialog handler the instant a dialog appears. If no handler is registered, Vibium's default behavior keeps your script from hanging — but register one explicitly whenever a click might open a dialog, so you control whether it's accepted or dismissed. To learn how Vibium's BiDi foundation enables this, see What is WebDriver BiDi?.
Next steps
Frequently asked questions
How do I handle a JavaScript alert in Vibium?
Register a handler before the dialog fires with vibe.on_dialog(handler). When an alert, confirm, or prompt opens, Vibium calls your handler with a dialog object, and you call dialog.accept() or dialog.dismiss() to respond. Set the handler up before the action that triggers the dialog.
How do I read the text of a dialog in Vibium?
Inside your on_dialog handler, call dialog.message() to get the dialog's text and dialog.type() to learn whether it is an alert, confirm, prompt, or beforeunload. This lets you assert on the message or branch your logic before accepting or dismissing the dialog.
How do I type into a prompt dialog with Vibium?
Pass the text you want as an argument to accept, like dialog.accept('my answer'). For a prompt dialog, that string becomes the prompt's return value. For alerts and confirms the text argument is ignored, and dialog.dismiss() cancels the prompt entirely.
Vibium is created by Jason Huggins. This is an independent tutorial — see the official Vibium site and GitHub repo for canonical docs.
Related guides
How to Check a Checkbox in Vibium
Check a checkbox in Vibium with el.check(). Runnable Python examples for ticking, unticking, verifying state, and handling many checkboxes.
3 min read→Commands & APIHow to Clear an Input Field in Vibium
Clear an input field in Vibium with el.clear(). Runnable Python examples for emptying text fields, replacing values, and resetting forms.
2 min read→Commands & APIHow to Click Elements in Vibium
Click buttons and links in Vibium with el.click(). Runnable Python examples plus how auto-waiting and actionability make clicks reliable.
2 min read→Commands & APIHow to Count Elements in Vibium
Count elements in Vibium with el.count() or len(findAll()). Runnable Python examples for counting rows, links, search results, and assertions.
3 min read→