如何处理剧作家中的铬麦克风权限弹出窗口?

How to handle chromium microphone permission pop-ups in playwright?

我想做什么

使用剧作家测试需要麦克风访问权限的网站

问题

出现问题的弹出窗口似乎忽略了理应授予的权限。可以手动授予权限,但这似乎违背了自动化的精神。

我试过的

with sync_playwright() as p:

    browser = p.chromium.launch(headless=False)
    context = browser.new_context(permissions=['microphone'])
...

由于某些原因,无法通过上下文授予权限。权限弹出窗口仍然出现。

我也试过用剧作家录制脚本录制演练,但它不是录制授予麦克风权限。

您缺少一些命令行 flags 告诉 chrome 模拟拥有麦克风。试一试这个样本。

from playwright.sync_api import sync_playwright


def run(playwright):
    chromium = playwright.chromium
    browser = chromium.launch(headless=False, args=['--use-fake-device-for-media-stream', '--use-fake-ui-for-media-stream'])
    context = browser.new_context()
    context.grant_permissions(permissions=['microphone'])
    page = context.new_page()
    page.goto("https://permission.site/")
    page.click('#microphone')
    page.pause()
    # other actions...
    browser.close()


with sync_playwright() as playwright:
    run(playwright)