# Python Selenium の自動化

これは、ローカル API 経由で MoreLogin プロファイルを開始し、[Selenium WebDriver](https://www.selenium.dev/) を使用してそれに接続する方法を示す、完全な実行可能な Python の例です。

> **注意**: これには、MoreLogin クライアントが実行されており、ローカルでログインしている必要があります。


## 前提条件

```bash
pip install requests selenium
```

## スクリプト例

```python
"""
Description:
Used to start the profile browser, then connect to the browser with webdriver and perform a Google search.
Note: To use this interface, you need to start the MoreLogin client and successfully log in.
"""

import time
import requests
import traceback
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

def main():
    try:
        # browser's order num, you can get it from profile list page: Numerical order
        unique_id = 1
        # browser profile id
        env_id = ""
        debug_url, webdriver_path = start(env_id, unique_id)
        if debug_url is None or webdriver_path is None:
            return
            
        driver = connect(debug_url, webdriver_path)
        operation(driver)

        # wait 10 seconds
        time.sleep(10)

        # try close env
        stop(env_id, unique_id)
        print("env closed")
    except Exception:
        error = traceback.format_exc()
        print("run-error: " + error)


# connect webdriver with exist browser
def connect(debug_url, webdriver_path):
    print("connecting to " + webdriver_path)

    opts = Options()
    opts.add_experimental_option("debuggerAddress", debug_url)

    # Use the specific chromedriver provided by MoreLogin
    service = Service(executable_path=webdriver_path)
    driver = webdriver.Chrome(service=service, options=opts)
    return driver


# start a browser profile, and return debug-url and webdriver path
def start(envId, uniqueId):
    # Send the envId(profile ID) or the uniqueId(profile order number).
    # If both are sent, the profile ID takes precedence.
    data = {"envId": envId, "uniqueId": uniqueId}
    response = requests.post("http://localhost:40000/api/env/start", json=data).json()

    if response["code"] != 0:
        print(f"code: {response['code']}, error: {response['msg']}, request_id: {response['requestId']}")
        print("please check envId")
        return None, None

    return "127.0.0.1:" + response["data"]["debugPort"], response["data"]["webdriver"]


# open page and operation
def operation(driver):
    # new tab, and open google for search
    driver.switch_to.new_window("tab")
    driver.get("https://www.google.com")

    # wait for page loaded
    WebDriverWait(driver, 10)

    # find input element and fill word
    element = driver.find_element("css selector", '[name="q"]')
    element.send_keys("MoreLogin")
    element.send_keys(Keys.RETURN)
    print("search executed")


# close a browser profile
def stop(envId, uniqueId):
    data = {"envId": envId, "uniqueId": uniqueId}
    response = requests.post("http://localhost:40000/api/env/close", json=data).json()
    if response["code"] == -1:
        print(f"code: {response['code']}, error: {response['msg']}, request_id: {response['requestId']}")
        return False
    return True

if __name__ == "__main__":
    main()
```