playwright
playwright copied to clipboard
[Feature] Add an option for mute that is similar to headless.
The Playwright documentation has a link to args for Chromium but not WebKit. I looked on the WebKit site but could not find a list. I googled for a similar code but found none that dealt with WebKit and sound. No matter what args I try, the sound keeps playing. How can the sound be muted? Help, please.
from playwright.sync_api import sync_playwright
with sync_playwright() as playwright:
# TODO mute the browser; I can't find a list of WebKit args, and none of my guesses stop the sound
# args=['--nosound'] ignore_default_args=['--nosound']) args=["--mute-audio"]) ignore_default_args=["--mute-audio"]
with playwright.webkit.launch(headless=False, ignore_default_args=["--mute-audio"]) as browser:
with browser.new_context() as context:
with context.new_page() as page:
page.goto("https://www.youtube.com/watch?v=J2O_xa8cems&list=RDJ2O_xa8cems&start_radio=1")
page.wait_for_timeout(40000) # 30 seconds of ads then 10 seconds of the music video
There is no such option in WebKit. I'll leave it open as a feature request. Can you tell us a bit more about your use case and why you need it?
I use Playwright to automate something repetitive that I need to do on a noisy website. I settled on using WebKit because the website renders inconsistently with Chromium and Firefox. I run the automation headless while doing other work, and the random noises disturb my concentration.
I get that most people probably use Playwriter for testing. Imagine someone running current tests on a noisy website. Oh my, the overlapping audio would be awful.
Turning off all the sound on the computer is not practical because someone could, for example, initiate a video conference while you happen to be running Playwright.
A website has the stuff to see and stuff to hear. Currently, Playwright has a headless option to suppress visuals but nothing for sounds.
Instead of a WebKit-specific muting arg, it would be lovely if the launch method had a browser-agnostic keyword argument mute=True or False, which defaults to False for backward compatibility.
+1 for this. I want to be able to demo concurrently running tests on a video call. The audio and text-to-speech involved is overwhelming, and there's no apparent workaround.
If you just want to temporarily avoid this issue, you can consider injecting a JavaScript script
like this:
AUDIO_MUTE_JS_SCRIPT = """() => {
function disableVideoFeatures(video) {
video.autoplay = false;
video.muted = true;
video.src = '';
}
let videos_playwright = document.getElementsByTagName('video');
for (let i = 0; i < videos_playwright.length; i++) {
disableVideoFeatures(videos_playwright[i]);
}
let observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node) {
if (node instanceof HTMLVideoElement) {
disableVideoFeatures(node);
} else if (node.getElementsByTagName) {
let videos = node.getElementsByTagName('video');
for (let i = 0; i < videos.length; i++) {
disableVideoFeatures(videos[i]);
}
}
});
});
});
let config_playwright = { childList: true, subtree: true };
observer.observe(document.body, config_playwright);
}"""
await self.page.goto(base_url)
await self.page.evaluate(AUDIO_MUTE_JS_SCRIPT)
+1
+1
+1
Why was this issue closed?
Thank you for your involvement. This issue was closed due to limited engagement (upvotes/activity), lack of recent activity, and insufficient actionability. To maintain a manageable database, we prioritize issues based on these factors.
If you have additional information not present in this issue that you think will help prioritizing it, please open a new issue and reference this one. More support or clarity on its necessity may prompt a review. Your understanding and cooperation are appreciated.