[파이썬] Python Playwright로 Edge 브라우저 제어하기

- 16:58
- 219 회
- 0 건
Python에서 Playwright를 사용하면 Chromium 기반의 Edge 브라우저를 다양한 방식으로 제어할 수 있습니다.
이 글에서는 브라우저를 실행하거나 연결하는 대표적인 방법 세 가지를 정리했습니다.
1. launch
– 새 브라우저 실행
async with async_playwright() as p:
browser = await p.chromium.launch(
channel="msedge",
headless=False,
args=[
"--remote-debugging-port=9222", # 선택 사항
"--start-maximized",
"--disable-infobars",
],
)
context = await browser.new_context(no_viewport=True)
page = await context.new_page()
- 완전히 새로운 브라우저 프로세스를 실행합니다.
--remote-debugging-port
는 Playwright 단독 사용 시 필요하지 않지만, Puppeteer나 DevTools 같은 다른 툴과 병행할 때 유용합니다.- 항상 깨끗한 환경에서 시작할 수 있다는 장점이 있습니다.
2. connect_over_cdp
– 실행 중인 브라우저에 접속
async with async_playwright() as p:
browser = await p.chromium.connect_over_cdp("http://127.0.0.1:9222")
context = browser.contexts[0]
page = context.pages[0] if context.pages else await context.new_page()
- 이미 실행 중인 브라우저에 연결해 제어할 수 있습니다.
- 이 방식을 쓰려면 브라우저를
--remote-debugging-port=9222
옵션과 함께 실행해야 합니다. - 기존에 열려 있던 탭, 세션 등을 그대로 활용할 수 있습니다.
3. launch_persistent_context
– 프로필 기반 실행
async with async_playwright() as p:
context = await p.chromium.launch_persistent_context(
channel="msedge",
user_data_dir=data_dir,
headless=False,
args=[
"--start-maximized",
"--disable-infobars",
"--test-type"
],
ignore_https_errors=True,
no_viewport=True,
)
page = context.pages[0] if context.pages else await context.new_page()
user_data_dir
을 지정해 특정 프로필 디렉토리를 사용합니다.- 쿠키, 로그인 상태, 확장 프로그램 등을 유지하면서 자동화를 실행할 수 있습니다.
- 이 방식에서는
remote-debugging-port
옵션이 필요하지 않습니다.
로그인 후 댓글내용을 입력해주세요