from pathlib import Path from tempfile import TemporaryDirectory import shutil from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait HTML = """
waiting
""" options = Options() options.add_argument("--headless=new") options.add_argument("--window-size=1280,720") for browser_name in ("google-chrome", "chromium", "chromium-browser"): browser_path = shutil.which(browser_name) if browser_path: options.binary_location = browser_path break driver_path = shutil.which("chromedriver") service = Service(driver_path) if driver_path else Service() with TemporaryDirectory() as tmpdir: page = Path(tmpdir) / "form-submit.html" page.write_text(HTML, encoding="utf-8") driver = webdriver.Chrome(service=service, options=options) try: driver.get(page.as_uri()) wait = WebDriverWait(driver, 10) full_name = wait.until( EC.visibility_of_element_located((By.NAME, "full_name")) ) email = driver.find_element(By.NAME, "email") message = driver.find_element(By.NAME, "message") submit = wait.until( EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']")) ) full_name.clear() full_name.send_keys("Aisha Rahman") email.clear() email.send_keys("aisha@example.test") message.clear() message.send_keys("Please send the invoice.") submit.click() wait.until( EC.text_to_be_present_in_element((By.ID, "result"), "submitted") ) result = driver.find_element(By.ID, "result").text submitted = driver.find_element(By.TAG_NAME, "body").get_attribute( "data-submitted" ) print(f"title: {driver.title}") print(f"submitted: {submitted}") print(f"result: {result}") finally: driver.quit()