前言
最近用小伙伴提到多线程同时操作多个浏览器,每个浏览器用不同账号登录的需求。
多线程操作
以下是2个账号同时打开2个浏览器操作的示例
from playwright.sync_api import sync_playwright
from threading import Thread
def do_some_thing(username, password):
playwright = sync_playwright().start()
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto("http://127.0.0.1/login.html")
page.get_by_placeholder("请输入用户名").fill(username)
page.get_by_placeholder("请输入密码").fill(password)
page.get_by_role("button", name="立即登录 >").click()
# do ....
context.close()
browser.close()
users = [
["yoyo", "123456**"],
["yoyo2", "123456**"]
]
# 多线程,2个账号同时操作
for user in users:
thread = Thread(target=do_some_thing, args=user)
thread.start()
标签:playwright,浏览器,账号,python,context,多线程,page
From: https://www.cnblogs.com/yoyoketang/p/17426724.html