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 = """
Loading form
""" 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) / "explicit-wait-demo.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, poll_frequency=0.2) wait.until( EC.text_to_be_present_in_element((By.ID, "status"), "Ready to submit"), "status text did not show the form was ready", ) button = wait.until( EC.element_to_be_clickable((By.ID, "submit-order")), "submit button did not become clickable", ) button.click() wait.until( EC.text_to_be_present_in_element((By.ID, "status"), "Submitted"), "status text did not show submission", ) print(f"title: {driver.title}") print(f"button_text: {button.text}") print(f"status: {driver.find_element(By.ID, 'status').text}") finally: driver.quit()