<h3 dir="auto">System info</h3>
<ul dir="auto">
<li>Playwright Version: [v1.31.2]</li>
<li>Operating System: [macOS 13.2.1]</li>
<li>Browser: [Chromium]</li>
<li>Other info: [Node 16.16.0]</li>
</ul>
<h3 dir="auto">Source code</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I provided exact source code that allows reproducing the issue locally.</li>
</ul>




<p dir="auto"><strong>Test file (self-contained)</strong></p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="const { chromium } = require('playwright');
(async () =&gt; {
  const browser = await chromium.launch({
    devtools: true
  });
  const context = await browser.newContext();
  const page = await context.newPage();

  await page.goto('https://example.com');

  page.on('dialog', async dialog =&gt; {
    console.log(`Found dialog. Type: ${dialog.type()}, message: ${dialog.message()}`); // get dialog type and message
    // await dialog.dismiss(); // Does not close the dialog and does not navigate
    await dialog.accept(); // Does not close the dialog but allows the navigation
  })

  await page.addScriptTag({ content: 'window.onbeforeunload = (evt) =&gt; { evt.preventDefault(); evt.returnValue = &quot;&quot;}' })

  await page.locator('body').click(); // chromium requires a user gesture to enable beforeunload

  await page.evaluate('window.location=&quot;https://www.iana.org/domains/reserved&quot;')

  await page.waitForTimeout(3000) // wait to check if the dialog disappears

  await context.close();
  await browser.close();
})();"><pre class="notranslate"><span class="pl-k">const</span> <span class="pl-kos">{</span> chromium <span class="pl-kos">}</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">'playwright'</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">(</span><span class="pl-k">async</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
  <span class="pl-k">const</span> <span class="pl-s1">browser</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-s1">chromium</span><span class="pl-kos">.</span><span class="pl-en">launch</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
    <span class="pl-c1">devtools</span>: <span class="pl-c1">true</span>
  <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-k">const</span> <span class="pl-s1">context</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-s1">browser</span><span class="pl-kos">.</span><span class="pl-en">newContext</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-k">const</span> <span class="pl-s1">page</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-s1">context</span><span class="pl-kos">.</span><span class="pl-en">newPage</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

  <span class="pl-k">await</span> <span class="pl-s1">page</span><span class="pl-kos">.</span><span class="pl-en">goto</span><span class="pl-kos">(</span><span class="pl-s">'https://example.com'</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

  <span class="pl-s1">page</span><span class="pl-kos">.</span><span class="pl-en">on</span><span class="pl-kos">(</span><span class="pl-s">'dialog'</span><span class="pl-kos">,</span> <span class="pl-k">async</span> <span class="pl-s1">dialog</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">`Found dialog. Type: <span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">dialog</span><span class="pl-kos">.</span><span class="pl-en">type</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">}</span></span>, message: <span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">dialog</span><span class="pl-kos">.</span><span class="pl-en">message</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">}</span></span>`</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// get dialog type and message</span>
    <span class="pl-c">// await dialog.dismiss(); // Does not close the dialog and does not navigate</span>
    <span class="pl-k">await</span> <span class="pl-s1">dialog</span><span class="pl-kos">.</span><span class="pl-en">accept</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// Does not close the dialog but allows the navigation</span>
  <span class="pl-kos">}</span><span class="pl-kos">)</span>

  <span class="pl-k">await</span> <span class="pl-s1">page</span><span class="pl-kos">.</span><span class="pl-en">addScriptTag</span><span class="pl-kos">(</span><span class="pl-kos">{</span> <span class="pl-c1">content</span>: <span class="pl-s">'window.onbeforeunload = (evt) =&gt; { evt.preventDefault(); evt.returnValue = ""}'</span> <span class="pl-kos">}</span><span class="pl-kos">)</span>

  <span class="pl-k">await</span> <span class="pl-s1">page</span><span class="pl-kos">.</span><span class="pl-en">locator</span><span class="pl-kos">(</span><span class="pl-s">'body'</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">click</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// chromium requires a user gesture to enable beforeunload</span>

  <span class="pl-k">await</span> <span class="pl-s1">page</span><span class="pl-kos">.</span><span class="pl-en">evaluate</span><span class="pl-kos">(</span><span class="pl-s">'window.location="https://www.iana.org/domains/reserved"'</span><span class="pl-kos">)</span>

  <span class="pl-k">await</span> <span class="pl-s1">page</span><span class="pl-kos">.</span><span class="pl-en">waitForTimeout</span><span class="pl-kos">(</span><span class="pl-c1">3000</span><span class="pl-kos">)</span> <span class="pl-c">// wait to check if the dialog disappears</span>

  <span class="pl-k">await</span> <span class="pl-s1">context</span><span class="pl-kos">.</span><span class="pl-en">close</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-k">await</span> <span class="pl-s1">browser</span><span class="pl-kos">.</span><span class="pl-en">close</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto"><strong>Steps</strong></p>
<ul dir="auto">
<li>[Run the script]</li>
</ul>
<p dir="auto"><strong>Expected</strong></p>
<p dir="auto">If the dialog is dismissed, the dialog should close and the navigation should fail and the browser closes<br>
If the dialog is accepted, the dialog should close and the navigation should occur and the browser closes</p>
<p dir="auto"><strong>Actual</strong></p>
<p dir="auto">If the dialog is dismissed, the dialog does NOT close. The navigation does not occur and the browser does NOT close<br>
If the dialog is accepted, the dialog does NOT close, although the navigation does occur and the browser does close</p>
<p dir="auto"><strong>Context:</strong></p>
<ul dir="auto">
<li>Playwright Version: 1.25.0</li>
<li>Operating System: Windows 10 x64</li>
<li>.NET version: .NET 6</li>
<li>Browser: MS Edge</li>
<li>Extra: Visual Studio Enterprise 17.3.2</li>
</ul>
<p dir="auto"><strong>Code Snippet</strong></p>
<div class="highlight highlight-source-cs notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="[TestMethod]
public async Task LoadPlaywrightPage()
{
    var playwright = await Playwright.CreateAsync();
    var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
    {
        Headless = false,
        Channel = &quot;msedge&quot;
    });
    var context = await browser.NewContextAsync();
    var page = await context.NewPageAsync();
    await page.GotoAsync(&quot;https://playwright.dev/dotnet/&quot;);

    Assert.IsTrue((await page.TitleAsync()).Contains(&quot;Playwright .NET&quot;));
}"><pre class="notranslate"><span class="pl-kos">[</span><span class="pl-c1">TestMethod</span><span class="pl-kos">]</span>
<span class="pl-k">public</span> <span class="pl-k">async</span> Task <span class="pl-en">LoadPlaywrightPage</span><span class="pl-kos">(</span><span class="pl-kos">)</span>
<span class="pl-kos">{</span>
    <span class="pl-smi">var</span> <span class="pl-s1">playwright</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> Playwright<span class="pl-kos">.</span><span class="pl-en">CreateAsync</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-smi">var</span> <span class="pl-s1">browser</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> playwright<span class="pl-kos">.</span>Chromium<span class="pl-kos">.</span><span class="pl-en">LaunchAsync</span><span class="pl-kos">(</span><span class="pl-k">new</span> BrowserTypeLaunchOptions
    <span class="pl-kos">{</span>
        <span class="pl-s1">Headless</span> <span class="pl-c1">=</span> <span class="pl-c1">false</span><span class="pl-kos">,</span>
        <span class="pl-s1">Channel</span> <span class="pl-c1">=</span> <span class="pl-s"><span class="pl-s">"</span>msedge<span class="pl-s">"</span></span>
    <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-smi">var</span> <span class="pl-s1">context</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> browser<span class="pl-kos">.</span><span class="pl-en">NewContextAsync</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-smi">var</span> <span class="pl-s1">page</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> context<span class="pl-kos">.</span><span class="pl-en">NewPageAsync</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">await</span> page<span class="pl-kos">.</span><span class="pl-en">GotoAsync</span><span class="pl-kos">(</span><span class="pl-s"><span class="pl-s">"</span>https://playwright.dev/dotnet/<span class="pl-s">"</span></span><span class="pl-kos">)</span><span class="pl-kos">;</span>

    Assert<span class="pl-kos">.</span><span class="pl-en">IsTrue</span><span class="pl-kos">(</span><span class="pl-kos">(</span><span class="pl-k">await</span> page<span class="pl-kos">.</span><span class="pl-en">TitleAsync</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">Contains</span><span class="pl-kos">(</span><span class="pl-s"><span class="pl-s">"</span>Playwright .NET<span class="pl-s">"</span></span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto"><strong>Describe the bug</strong></p>
<p dir="auto">Running Visual Studio Enterprise in non-admin, the above code throws this exception:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Test method PlaywrightTests.PlaywrightTests.LoadPlaywrightPage threw exception: 
   Microsoft.Playwright.PlaywrightException: Browser closed. 
==================== Browser output: ==================== 
C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe --disable-field-trial-config --disable-background-networking 
--enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling 
--disable-backgrounding-occluded-windows --disable-back-forward-cache --disable-breakpad 
--disable-client-side-phishing-detection --disable-component-extensions-with-background-pages 
--disable-default-apps --disable-dev-shm-usage --disable-extensions 
--disable-features=ImprovedCookieControls,LazyFrameLoading,GlobalMediaControls,DestroyProfileOnBrowserClose,
MediaRouter,DialMediaRouteProvider,AcceptCHFrame,AutoExpandDetailsElement,
CertificateTransparencyComponentUpdater,AvoidUnnecessaryBeforeUnloadCheckSync,Translate 
--allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking 
--disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb 
--metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain 
--no-service-autorun --export-tagged-pdf --no-sandbox 
--user-data-dir=C:\Users\MyUserId\AppData\Local\Temp\1\playwright_chromiumdev_profile-XJmwuh 
--remote-debugging-pipe --no-startup-window pid=35316 [pid=35316][out] Opening in existing browser session. 
=========================== logs =========================== 
C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe --disable-field-trial-config --disable-background-networking 
--enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling 
--disable-backgrounding-occluded-windows --disable-back-forward-cache --disable-breakpad 
--disable-client-side-phishing-detection --disable-component-extensions-with-background-pages 
--disable-default-apps --disable-dev-shm-usage --disable-extensions 
--disable-features=ImprovedCookieControls,LazyFrameLoading,GlobalMediaControls,DestroyProfileOnBrowserClose,
MediaRouter,DialMediaRouteProvider,AcceptCHFrame,AutoExpandDetailsElement,
CertificateTransparencyComponentUpdater,AvoidUnnecessaryBeforeUnloadCheckSync,Translate 
--allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking 
--disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb 
--metrics-recording-only --no-first-run --enable-automation --password-store=basic 
--use-mock-keychain --no-service-autorun --export-tagged-pdf --no-sandbox 
--user-data-dir=C:\Users\MyUserId\AppData\Local\Temp\1\playwright_chromiumdev_profile-XJmwuh 
--remote-debugging-pipe --no-startup-window pid=35316 [pid=35316][out] Opening in existing browser session. 
============================================================ 
at Microsoft.Playwright.Transport.Connection.InnerSendMessageToServerAsync[T](String guid, String method, Object args) in 
//src/Playwright/Transport/Connection.cs:line 167 at Microsoft.Playwright.Transport.Connection.WrapApiCallAsync[T](Func`1 
action, Boolean isInternal) at Microsoft.Playwright.Core.BrowserType.LaunchAsync(BrowserTypeLaunchOptions options) in 
//src/Playwright/Core/BrowserType.cs:line 61 at PlaywrightTests.PlaywrightTests.LoadPlaywrightPage() in 
C:\MyProjects\PlaywrightPlayground\PlaywrightTests\PlaywrightTests.cs:line 115 at 
Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.ThreadOperations.ExecuteWithAbortSafety(Action action)
"><pre class="notranslate"><code class="notranslate">Test method PlaywrightTests.PlaywrightTests.LoadPlaywrightPage threw exception: 
   Microsoft.Playwright.PlaywrightException: Browser closed. 
==================== Browser output: ==================== 
C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe --disable-field-trial-config --disable-background-networking 
--enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling 
--disable-backgrounding-occluded-windows --disable-back-forward-cache --disable-breakpad 
--disable-client-side-phishing-detection --disable-component-extensions-with-background-pages 
--disable-default-apps --disable-dev-shm-usage --disable-extensions 
--disable-features=ImprovedCookieControls,LazyFrameLoading,GlobalMediaControls,DestroyProfileOnBrowserClose,
MediaRouter,DialMediaRouteProvider,AcceptCHFrame,AutoExpandDetailsElement,
CertificateTransparencyComponentUpdater,AvoidUnnecessaryBeforeUnloadCheckSync,Translate 
--allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking 
--disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb 
--metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain 
--no-service-autorun --export-tagged-pdf --no-sandbox 
--user-data-dir=C:\Users\MyUserId\AppData\Local\Temp\1\playwright_chromiumdev_profile-XJmwuh 
--remote-debugging-pipe --no-startup-window pid=35316 [pid=35316][out] Opening in existing browser session. 
=========================== logs =========================== 
C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe --disable-field-trial-config --disable-background-networking 
--enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling 
--disable-backgrounding-occluded-windows --disable-back-forward-cache --disable-breakpad 
--disable-client-side-phishing-detection --disable-component-extensions-with-background-pages 
--disable-default-apps --disable-dev-shm-usage --disable-extensions 
--disable-features=ImprovedCookieControls,LazyFrameLoading,GlobalMediaControls,DestroyProfileOnBrowserClose,
MediaRouter,DialMediaRouteProvider,AcceptCHFrame,AutoExpandDetailsElement,
CertificateTransparencyComponentUpdater,AvoidUnnecessaryBeforeUnloadCheckSync,Translate 
--allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking 
--disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb 
--metrics-recording-only --no-first-run --enable-automation --password-store=basic 
--use-mock-keychain --no-service-autorun --export-tagged-pdf --no-sandbox 
--user-data-dir=C:\Users\MyUserId\AppData\Local\Temp\1\playwright_chromiumdev_profile-XJmwuh 
--remote-debugging-pipe --no-startup-window pid=35316 [pid=35316][out] Opening in existing browser session. 
============================================================ 
at Microsoft.Playwright.Transport.Connection.InnerSendMessageToServerAsync[T](String guid, String method, Object args) in 
//src/Playwright/Transport/Connection.cs:line 167 at Microsoft.Playwright.Transport.Connection.WrapApiCallAsync[T](Func`1 
action, Boolean isInternal) at Microsoft.Playwright.Core.BrowserType.LaunchAsync(BrowserTypeLaunchOptions options) in 
//src/Playwright/Core/BrowserType.cs:line 61 at PlaywrightTests.PlaywrightTests.LoadPlaywrightPage() in 
C:\MyProjects\PlaywrightPlayground\PlaywrightTests\PlaywrightTests.cs:line 115 at 
Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.ThreadOperations.ExecuteWithAbortSafety(Action action)

</code></pre></div>
<p dir="auto">If I open Visual Studio with "Run as administrator", the test runs fine and MS Edge opens successfully. I have a Stack Overflow post about this here: <a href="https://stackoverflow.com/questions/73651755/playwright-throws-exception-browser-closed-when-testing-against-ms-edge/" rel="nofollow">https://stackoverflow.com/questions/73651755/playwright-throws-exception-browser-closed-when-testing-against-ms-edge/</a></p>
<p dir="auto">Thank you.</p>