from pathlib import Path from tempfile import TemporaryDirectory import os 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 CSS selector demo

Starter

$9

""" options = Options() options.add_argument("--headless=new") options.add_argument("--disable-dev-shm-usage") options.add_argument("--window-size=1280,720") if hasattr(os, "geteuid") and os.geteuid() == 0: options.add_argument("--no-sandbox") 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) / "pricing.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) pro_plan = wait.until( EC.visibility_of_element_located( (By.CSS_SELECTOR, "[data-testid='pricing-plan'][data-plan='pro']") ) ) price = pro_plan.find_element(By.CSS_SELECTOR, ".plan-price").text active_plans = driver.find_elements( By.CSS_SELECTOR, "[data-testid='pricing-plan'][data-active='true']" ) print(f"title: {driver.title}") print(f"selected_plan: {pro_plan.find_element(By.CSS_SELECTOR, '.plan-title').text}") print(f"selected_price: {price}") print(f"active_plan_count: {len(active_plans)}") finally: driver.quit()