{"templateId":"markdown","sharedDataIds":{"sidebar":"sidebar-@l10n/vi/sidebars.yaml"},"props":{"metadata":{"markdoc":{"tagList":[]},"type":"markdown"},"seo":{"title":"Tự động hóa Python Selenium","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":"tự-động-hóa-python-selenium","__idx":0},"children":["Tự động hóa Python Selenium"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Đây là ví dụ Python hoàn chỉnh, có thể thực thi, minh họa cách khởi động cấu hình MoreLogin thông qua API cục bộ và kết nối với cấu hình đó bằng ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://www.selenium.dev/"},"children":["Selenium WebDriver"]},"."]},{"$$mdtype":"Tag","name":"blockquote","attributes":{},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Lưu ý"]},": Điều này yêu cầu ứng dụng khách MoreLogin phải chạy và đăng nhập cục bộ."]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"điều-kiện-tiên-quyết","__idx":1},"children":["Điều kiện tiên quyết"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"bash","header":{"controls":{"copy":{}}},"source":"pip install requests selenium\n","lang":"bash"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"tập-lệnh-mẫu","__idx":2},"children":["Tập lệnh mẫu"]},{"$$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 webdriver and perform a Google search.\nNote: To use this interface, you need to start the MoreLogin client and successfully log in.\n\"\"\"\n\nimport time\nimport requests\nimport traceback\nfrom selenium import webdriver\nfrom selenium.webdriver.support.ui import WebDriverWait\nfrom selenium.webdriver.common.keys import Keys\nfrom selenium.webdriver.chrome.service import Service\nfrom selenium.webdriver.chrome.options import Options\n\ndef main():\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\n        env_id = \"\"\n        debug_url, webdriver_path = start(env_id, unique_id)\n        if debug_url is None or webdriver_path is None:\n            return\n            \n        driver = connect(debug_url, webdriver_path)\n        operation(driver)\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    except Exception:\n        error = traceback.format_exc()\n        print(\"run-error: \" + error)\n\n\n# connect webdriver with exist browser\ndef connect(debug_url, webdriver_path):\n    print(\"connecting to \" + webdriver_path)\n\n    opts = Options()\n    opts.add_experimental_option(\"debuggerAddress\", debug_url)\n\n    # Use the specific chromedriver provided by MoreLogin\n    service = Service(executable_path=webdriver_path)\n    driver = webdriver.Chrome(service=service, options=opts)\n    return driver\n\n\n# start a browser profile, and return debug-url and webdriver path\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, None\n\n    return \"127.0.0.1:\" + response[\"data\"][\"debugPort\"], response[\"data\"][\"webdriver\"]\n\n\n# open page and operation\ndef operation(driver):\n    # new tab, and open google for search\n    driver.switch_to.new_window(\"tab\")\n    driver.get(\"https://www.google.com\")\n\n    # wait for page loaded\n    WebDriverWait(driver, 10)\n\n    # find input element and fill word\n    element = driver.find_element(\"css selector\", '[name=\"q\"]')\n    element.send_keys(\"MoreLogin\")\n    element.send_keys(Keys.RETURN)\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":"Tự động hóa Python Selenium","id":"tự-động-hóa-python-selenium","depth":1},{"value":"Điều kiện tiên quyết","id":"điều-kiện-tiên-quyết","depth":2},{"value":"Tập lệnh mẫu","id":"tập-lệnh-mẫu","depth":2}],"frontmatter":{"seo":{"title":"Tự động hóa Python Selenium"}},"lastModified":"2026-06-10T11:47:34.000Z","pagePropGetterError":{"message":"","name":""}},"slug":"/vi/api-reference/examples/selenium","userData":{"isAuthenticated":false,"teams":["anonymous"]},"isPublic":true}