{"templateId":"markdown","sharedDataIds":{"sidebar":"sidebar-@l10n/es/sidebars.yaml"},"props":{"metadata":{"markdoc":{"tagList":[]},"type":"markdown"},"seo":{"title":"Automatización de Playwright en Python","llmstxt":{"hide":false,"sections":[{"title":"Table of contents","includeFiles":["**/*"],"excludeFiles":[]}],"excludeFiles":[]}},"dynamicMarkdocComponents":[],"compilationErrors":[],"ast":{"$$mdtype":"Tag","name":"article","attributes":{},"children":[{"$$mdtype":"Tag","name":"Heading","attributes":{"level":1,"id":"automatización-de-playwright-en-python","__idx":0},"children":["Automatización de Playwright en Python"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Este es un ejemplo completo y ejecutable de Python que demuestra cómo iniciar un perfil MoreLogin a través de la API local y conectarse a él usando ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://playwright.dev/python/"},"children":["Playwright"]},"."]},{"$$mdtype":"Tag","name":"blockquote","attributes":{},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Nota"]},": Esto requiere que el cliente MoreLogin (v2.32 o superior) esté ejecutándose y que haya iniciado sesión localmente."]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"requisitos-previos","__idx":1},"children":["Requisitos previos"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"bash","header":{"controls":{"copy":{}}},"source":"pip install requests playwright\nplaywright install\n","lang":"bash"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"guión-de-ejemplo","__idx":2},"children":["Guión de ejemplo"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"python","header":{"controls":{"copy":{}}},"source":"\"\"\"\nDescription:\nUsed to start the profile browser, then connect to the browser with Playwright over CDP and perform a Google search.\n\nNote: To use this interface, you need to start the MoreLogin client and successfully log in.\nMoreLogin version number cannot be lower than 2.32\n\"\"\"\n\nimport time\nimport requests\nimport traceback\nfrom playwright.sync_api import sync_playwright, Playwright\n\ndef main():\n    with sync_playwright() as playwright:\n        run(playwright)\n\ndef run(playwright: Playwright):\n    try:\n        # browser's order num, you can get it from profile list page: Numerical order\n        unique_id = 1\n        # browser profile id, please check API-demo: list_browser_profiles.py\n        env_id = \"\"\n        cdp_url = start(env_id, unique_id)\n\n        if cdp_url is None:\n            return\n\n        operation(cdp_url, playwright)\n\n        # wait 10 seconds\n        time.sleep(10)\n\n        # try close env\n        stop(env_id, unique_id)\n        print(\"env closed\")\n\n    except Exception:\n        error = traceback.format_exc()\n        print(\"run-error: \" + error)\n\n\n# start a browser profile, and return cdp-url\n# if browser already opened, the browser will auto bring to front\ndef start(envId, uniqueId):\n    # Send the envId(profile ID) or the uniqueId(profile order number).\n    # If both are sent, the profile ID takes precedence.\n    data = {\"envId\": envId, \"uniqueId\": uniqueId}\n    response = requests.post(\"http://localhost:40000/api/env/start\", json=data).json()\n\n    if response[\"code\"] != 0:\n        print(f\"code: {response['code']}, error: {response['msg']}, request_id: {response['requestId']}\")\n        print(\"please check envId\")\n        return None\n\n    port = response[\"data\"][\"debugPort\"]\n    print(\"env open result:\", response[\"data\"])\n    return \"http://127.0.0.1:\" + port\n\n\n# open page and operation\ndef operation(cdp_url, playwright):\n    browser = playwright.chromium.connect_over_cdp(cdp_url)\n    ctx = browser.contexts[0]\n\n    # try open page\n    page1 = ctx.new_page()\n    page1.goto(\"https://ipinfo.io\")\n\n    page2 = ctx.new_page()\n    page2.goto(\"https://www.google.com/\")\n    print(page2.title)\n    \n    # search for MoreLogin\n    page2.fill('[name=\"q\"]', \"MoreLogin\")\n    page2.press('[name=\"q\"]', \"Enter\")\n    print(\"search executed\")\n\n\n# close a browser profile\ndef stop(envId, uniqueId):\n    data = {\"envId\": envId, \"uniqueId\": uniqueId}\n    response = requests.post(\"http://localhost:40000/api/env/close\", json=data).json()\n    if response[\"code\"] == -1:\n        print(f\"code: {response['code']}, error: {response['msg']}, request_id: {response['requestId']}\")\n        return False\n    return True\n\nif __name__ == \"__main__\":\n    main()\n","lang":"python"},"children":[]}]},"headings":[{"value":"Automatización de Playwright en Python","id":"automatización-de-playwright-en-python","depth":1},{"value":"Requisitos previos","id":"requisitos-previos","depth":2},{"value":"Guión de ejemplo","id":"guión-de-ejemplo","depth":2}],"frontmatter":{"seo":{"title":"Automatización de Playwright en Python"}},"lastModified":"2026-06-10T11:47:34.000Z","pagePropGetterError":{"message":"","name":""}},"slug":"/es/api-reference/examples/playwright","userData":{"isAuthenticated":false,"teams":["anonymous"]},"isPublic":true}