Ví dụ về tự động hóa Selenium
Bài viết này được dịch tự động từ tiếng Anh. Chúng tôi sẽ hiệu đính và hoàn thiện nó sớm!
Tập lệnh tự động hóa đơn giản này trong Python sử dụng thư viện Selenium để thao tác hồ sơ trong Multilogin X Nó thực hiện các hành động sau:
- Đăng nhập bằng Multilogin X API
- Bắt đầu cấu hình xác định Selenium là loại tự động hóa được chọn
- Truy xuất cổng được sử dụng bởi hồ sơ đang chạy
- Khởi động trình điều khiển Selenium trên máy chủ cục bộ bằng cách sử dụng cổng đã lấy được
- Sử dụng trình điều khiển để điều khiển các hành động của trình duyệt
- Dừng hồ sơ trong 5 giây
Trước khi bạn bắt đầu
- Cài đặt các thư viện Python sau:
- yêu cầu
- selen
- Chèn các giá trị của bạn vào các biến bên dưới trong tập lệnh:
Chạy tập lệnh
- Đảm bảo rằng tác nhân được kết nối, vì nó giúp khởi chạy hồ sơ dễ dàng
- Theo mặc định, tập lệnh bên dưới hoạt động với Mimic . Để sử dụng cho Stealthfox , hãy thay
options=ChromiumOptions()
bằngoptions=Options()
trong dòng sau:driver = webdriver.Remote(command_executor=f'{LOCALHOST}:{selenium_port}', options=ChromiumOptions())
- Chạy tệp
.py
với mã tự động hóa của bạn
Ví dụ về kịch bản
import requests
import hashlib
import time
from selenium import webdriver
from selenium.webdriver.chromium.options import ChromiumOptions
from selenium.webdriver.firefox.options import Options
MLX_BASE = "https://api.multilogin.com"
MLX_LAUNCHER = "https://launcher.mlx.yt:45001/api/v1"
MLX_LAUNCHER_V2 = (
"https://launcher.mlx.yt:45001/api/v2" # recommended for launching profiles
)
LOCALHOST = "http://127.0.0.1"
HEADERS = {"Accept": "application/json", "Content-Type": "application/json"}
# TODO: Insert your account information in both variables below
USERNAME = ""
PASSWORD = ""
# TODO: Insert the Folder ID and the Profile ID below
FOLDER_ID = ""
PROFILE_ID = ""
def signin() -> str:
payload = {
"email": USERNAME,
"password": hashlib.md5(PASSWORD.encode()).hexdigest(),
}
r = requests.post(f"{MLX_BASE}/user/signin", json=payload)
if r.status_code != 200:
print(f"\nError during login: {r.text}\n")
else:
response = r.json()["data"]
token = response["token"]
return token
def start_profile() -> webdriver:
r = requests.get(
f"{MLX_LAUNCHER_V2}/profile/f/{FOLDER_ID}/p/{PROFILE_ID}/start?automation_type=selenium",
headers=HEADERS,
)
response = r.json()
if r.status_code != 200:
print(f"\nError while starting profile: {r.text}\n")
else:
print(f"\nProfile {PROFILE_ID} started.\n")
selenium_port = response["data"]["port"]
driver = webdriver.Remote(
command_executor=f"{LOCALHOST}:{selenium_port}", options=ChromiumOptions()
)
# For Stealthfox profiles use: options=Options()
# For Mimic profiles use: options=ChromiumOptions()
return driver
def stop_profile() -> None:
r = requests.get(f"{MLX_LAUNCHER}/profile/stop/p/{PROFILE_ID}", headers=HEADERS)
if r.status_code != 200:
print(f"\nError while stopping profile: {r.text}\n")
else:
print(f"\nProfile {PROFILE_ID} stopped.\n")
token = signin()
HEADERS.update({"Authorization": f"Bearer {token}"})
driver = start_profile()
driver.get("https://multilogin.com/")
time.sleep(5)
stop_profile()