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 = """ Selenium button click demo

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) / "button-click.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) button = wait.until(EC.element_to_be_clickable((By.ID, "place-order"))) button_text = button.text button.click() wait.until(EC.text_to_be_present_in_element((By.ID, "status"), "clicked")) status = driver.find_element(By.ID, "status").text print(f"title: {driver.title}") print(f"button_text: {button_text}") print(f"status: {status}") finally: driver.quit()