Tự động hóa với Selenium
Selenium vẫn là thư viện phổ biến nhất đối với cộng đồng Python khi nhắc đến tự động hóa trình duyệt. Mbbrowser hoàn toàn tương thích với các trình điều khiển của Selenium (WebDriver) để giúp bạn làm việc an toàn và hiệu quả.
Chuẩn bị
- Đảm bảo bạn đã cài thư viện Selenium:
pip install selenium. - Bật dịch vụ API trong Mbbrowser (Cổng mặc định: 54321).
Cơ chế hoạt động
Thay vì để Selenium tự mở một trình duyệt mới (sẽ lộ dấu vân tay thật của máy tính), chúng ta sẽ yêu cầu nó "kết nối" (Attach) vào cửa sổ trình duyệt mà Mbbrowser đã chuẩn bị sẵn.
Ví dụ cơ bản (Python)
python
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# 1. ID của môi trường bạn muốn mở
profile_id = "xxxx-xxxx-xxxx"
api_url = f"http://127.0.0.1:54321/api/v1/profile/start?id={profile_id}"
# 2. Gọi API để mở trình duyệt
resp = requests.get(api_url).json()
debugger_address = resp['data']['http_endpoint']
# 3. Kết nối Selenium vào trình duyệt đó
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", debugger_address)
driver = webdriver.Chrome(options=chrome_options)
# 4. Bắt đầu thao tác
driver.get("https://www.google.com")
print(driver.title)
# Khi xong, chỉ cần ngắt kết nối điều khiển
driver.quit()Sử dụng Selenium với Mbbrowser giúp bạn tận dụng được kho thư viện Python khổng lồ hiện có trong khi vẫn đảm bảo tính ẩn danh tuyệt đối.
