英文:
Connecting to an existing browser using Playwright
问题
在“库模式”下,我正在使用Playwright。典型的方法是:创建Playwright实例,启动浏览器,创建上下文,创建页面,前往URL。
但也有ConnectAsync和ConnectOverCDPAsync这两种方法,允许连接到现有的浏览器。
我什么时候会这样做呢?文档没有解释在哪些情况下我需要连接到现有的浏览器,而不是按照“入门”文档中所示的方式进行操作。连接到现有浏览器时,我会获得额外的功能吗?
英文:
I'm using Playwright in "library mode". The typical approach is: create Playwright instance, launch browser, create context, create page, go to URL.
But there are also ConnectAsync
and ConnectOverCDPAsync
which allow connection to an existing browser.
When would I do that? The docs don't explain in which cases I need to connect to an existing browser rather than doing it the way shown in the "getting started" docs? Do I get extra functionality when connecting to an existing browser?
答案1
得分: 3
我使用一个现有的浏览器会话,并与playwright库连接。我用它来进行网页抓取,有时需要手动更改浏览器中的一些设置,保持浏览器打开状态,然后运行我的playwright脚本,它会接管现有的浏览器会话。
首先,我使用一个批处理文件启动Chrome浏览器:
cd C:\Program Files\Google\Chrome\Application
chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\Users\Demo\chromepythondebug"
然后,将playwright与Chrome浏览器会话连接:
browser = playwright.chromium.connect_over_cdp("http://localhost:9222")
default_context = browser.contexts[0]
page = default_context.pages[0]
英文:
I use an existing browser session and connect playwright library with it. I use it for scraping, sometimes I have to manually change some setting in the browser, leave it open and then run my playwright script which then takes over the existing browser session.
I first start a chrome browser with a batch file
cd C:\Program Files\Google\Chrome\Application
chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\Users\Demo\chromepythondebug"
and then add connect playwright with the chrome browser session
browser = playwright.chromium.connect_over_cdp("http://localhost:9222")
default_context = browser.contexts[0]
page = default_context.pages[0]
答案2
得分: 1
我正在使用另一种方法,通过Python在我的现有浏览器上运行Playwright。
import os
from playwright.sync_api import sync_playwright
def start_my_browser():
custom_exe_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
if not os.path.exists(custom_exe_path):
raise FileExistsError("Chrome未找到,请先安装Chrome:{}".format(custom_exe_path))
with sync_playwright() as playwright:
browser = playwright.chromium.launch(headless=False, slow_mo=500, executable_path=custom_exe_path)
page = browser.new_page()
page.goto("http://playwright.dev")
print(page.title())
browser.close()
在Playwright网站的文档中,我搜索了很长时间,直到我看到中国网站上的一个视频,解释了如何使用"executable_path"来运行Playwright来启动我的现有浏览器。
英文:
I'm using another way to get Playwright to run on my existing browser using python.
import os
from playwright.sync_api import sync_playwright
def start_my_browser():
custom_exe_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
if not os.path.exists(custom_exe_path):
raise FileExistsError("Chrome not found {}, please install Chrome first".format(custom_exe_path))
with sync_playwright() as playwright:
browser = playwright.chromium.launch(headless=False, slow_mo=500, executable_path=custom_exe_path)
page = browser.new_page()
page.goto("http://playwright.dev")
print(page.title())
browser.close()
Search for very long how to get Playwright "fire up" my existing browser as the docs in Playwright website didn't really explain much until I saw a video on China website explaining using the "executable_path" to run.
答案3
得分: 1
- 启动创建一个新的进程。有时这在你的环境中是有限制的。
- 有外部服务,比如browserstack.com,提供连接到它们,保持你的测试非常轻量化,并享受这种客户端/服务器设计的其他优势。
英文:
- Launch create a new Process. Sometimes it is a limitation on your environments.
- There are external services like browserstack.com that offers connecting to them, keeping very light your Tests, parallelization, and other advantages of this client/server design.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论