This script opens a page with a button that triggers a prompt alert. It types 'Alice' into the prompt and accepts it. Then it prints the page text which should greet Alice.
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# Setup driver
options = webdriver.ChromeOptions()
options.add_argument('--headless=new')
driver = webdriver.Chrome(options=options)
try:
# Open a simple page with prompt alert
driver.get('data:text/html,<script>function showPrompt() { var result = prompt(\'Enter your name:\'); document.body.innerHTML = result ? `Hello, ${result}!` : 'No name entered.'; }</script><button onclick="showPrompt()">Click me</button>')
# Click the button to open prompt
button = driver.find_element(By.TAG_NAME, 'button')
button.click()
# Switch to prompt alert
alert = driver.switch_to.alert
# Send text input
alert.send_keys('Alice')
# Accept the prompt
alert.accept()
time.sleep(1) # Wait for page update
# Verify the page shows the input
body_text = driver.find_element(By.TAG_NAME, 'body').text
print(body_text)
finally:
driver.quit()