简体   繁体   中英

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

What I'm trying to do

Test a website that requires microphone access with playwright

The problem

Pop-up in question comes up and seems to ignore supposedly granted permissions. Permission can be given manually, but this seems against the spirit of automation.

在此处输入图像描述

What I tried

with sync_playwright() as p:

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

Granting permissions via context doesn't work for some reason. The permission pop-up still comes up.

I also tried to record a walkthrough with playwrights record script, but it's not recording granting microphone permissions.

You're missing some command line flags that tell chrome to simulate having a microphone. Give this sample a shot.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM