GLM 4.5 Air

Fix Puppeteer Sharp Hangs on NewPage Without DevTools

Learn how to fix Puppeteer Sharp hanging on NewPageAsync when developer tools are disabled. Complete guide with code solutions for Microsoft Edge browser automation.

Question

Puppeteer Sharp Hangs on NewPage When Developer Tools is Disabled

I’m experiencing an issue where Puppeteer Sharp hangs on the following line when developer tools are disabled:

csharp
var page = await browser.NewPageAsync();

On our machine, developer tools are disabled. Can the code be run without devtools enabled? Here’s my current implementation:

csharp
public static async Task RunDemo()
{
    // 1. Download the Chromium browser if it's not already present.
    // This ensures Puppeteer Sharp has a browser executable to control.
    // 2. Launch the browser in headless mode (no visible UI).
    // Set Headless = false to see the browser window during execution.

    string edgePath = GetEdgePath();
    var browser = await LaunchBrowserAsync(edgePath);
    
    // 3. Create a new page within the browser.
    var page = await browser.NewPageAsync();

    // 4. Navigate to a specified URL.
    await page.GoToAsync("https://www.google.com");

    // 5. Take a screenshot of the page and save it to a file.
    string screenshotPath = Path.Combine(Directory.GetCurrentDirectory(), "google_screenshot.png");
    await page.ScreenshotAsync(screenshotPath);

    // 6. Close the browser.
    await browser.CloseAsync();

    System.Console.WriteLine($"Screenshot saved to: {screenshotPath}");
}

public static async Task Main(string[] args)
{
    await RunDemo();
}

private static string GetEdgePath()
{
    string edgePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "Microsoft", "Edge", "Application", "msedge.exe");
    return edgePath;
}

private static async Task<Browser> LaunchBrowserAsync(string edgePath)
{
    var options = new LaunchOptions
    {
        Headless = false,
        ExecutablePath = edgePath,
        Devtools = true,
        Args = new[] { "--no-sandbox", "--disable-gpu" },
        DefaultViewport = new ViewPortOptions { Width = 2000, Height = 900 }
    };

    return (Browser)await Puppeteer.LaunchAsync(options);
}

How can I modify this code to work without developer tools enabled?

GLM 4.5 Air

Brief Answer

The issue occurs because Puppeteer Sharp requires specific configuration to work without developer tools. Modify your LaunchOptions to include Devtools = false and add the --disable-devtools argument to ensure the browser launches properly without devtools enabled.

Contents


Understanding the DevTools Issue

When Puppeteer Sharp attempts to create a new page with developer tools disabled, it can hang because the browser instance isn’t properly configured to communicate with Puppeteer without the devtools protocol. This is especially true when using Microsoft Edge instead of the standard Chromium browser.

The core issue lies in how Puppeteer establishes communication between your .NET code and the browser instance. Without proper configuration, this communication channel can fail, causing the NewPageAsync() call to hang indefinitely.

Important Note: Even if you set Devtools = false, you might still need to include specific command-line arguments to ensure the browser launches in a compatible mode.


Modified Code Solution

Here’s a modified version of your code that should work without developer tools enabled:

csharp
private static async Task<Browser> LaunchBrowserAsync(string edgePath)
{
    var options = new LaunchOptions
    {
        Headless = false, // Set to true if you want headless mode
        ExecutablePath = edgePath,
        Devtools = false, // Explicitly set to false
        Args = new[] 
        { 
            "--no-sandbox", 
            "--disable-gpu", 
            "--disable-devtools", // Explicitly disable devtools
            "--disable-extensions", // Disable extensions
            "--disable-background-timer-throttling",
            "--disable-renderer-backgrounding",
            "--disable-backgrounding-occluded-windows",
            "--disable-client-side-phishing-detection"
        },
        DefaultViewport = new ViewPortOptions { Width = 2000, Height = 900 }
    };

    return await Puppeteer.LaunchAsync(options);
}

Key Changes:

  1. Explicitly set Devtools = false - This ensures Puppeteer Sharp doesn’t attempt to establish a devtools connection.

  2. Added --disable-devtools argument - This forces Edge to launch without developer tools, even if system policies might otherwise enable them.

  3. Added additional compatibility arguments - These arguments help ensure a smoother experience when running without devtools.

If you still experience issues, try setting Headless = true to run in headless mode, which often works more reliably without devtools:

csharp
var options = new LaunchOptions
{
    Headless = true, // Try headless mode
    ExecutablePath = edgePath,
    Devtools = false,
    Args = new[] 
    { 
        "--no-sandbox", 
        "--disable-gpu", 
        "--disable-devtools",
        "--disable-extensions",
        "--disable-background-timer-throttling",
        "--disable-renderer-backgrounding"
    },
    DefaultViewport = new ViewPortOptions { Width = 2000, Height = 900 }
};

Alternative Approaches

If the above solution doesn’t work, consider these alternative approaches:

1. Use Chromium Instead of Edge

Sometimes using the standard Chromium browser instead of Edge provides better compatibility:

csharp
private static async Task<Browser> LaunchBrowserAsync()
{
    var options = new LaunchOptions
    {
        Headless = false,
        Devtools = false,
        Args = new[] 
        { 
            "--no-sandbox", 
            "--disable-gpu", 
            "--disable-devtools"
        },
        DefaultViewport = new ViewPortOptions { Width = 2000, Height = 900 }
    };

    return await Puppeteer.LaunchAsync(options);
}

2. Use Remote Debugging Port

You can try launching Edge with a remote debugging port:

csharp
private static async Task<Browser> LaunchBrowserAsync(string edgePath)
{
    var options = new LaunchOptions
    {
        Headless = false,
        ExecutablePath = edgePath,
        Devtools = false,
        Args = new[] 
        { 
            "--no-sandbox", 
            "--disable-gpu", 
            "--remote-debugging-port=9222",
            "--disable-devtools"
        },
        DefaultViewport = new ViewPortOptions { Width = 2000, Height = 900 }
    };

    return await Puppeteer.LaunchAsync(options);
}

3. Try the Puppeteer Sharp Standalone Version

The standalone version of Chromium bundled with Puppeteer Sharp might work better:

csharp
private static async Task<Browser> LaunchBrowserAsync()
{
    var options = new LaunchOptions
    {
        Headless = false,
        Devtools = false,
        Args = new[] 
        { 
            "--no-sandbox", 
            "--disable-gpu", 
            "--disable-devtools"
        },
        DefaultViewport = new ViewPortOptions { Width = 2000, Height = 900 },
        IgnoreHTTPSErrors = true
    };

    return await Puppeteer.LaunchAsync(options);
}

Troubleshooting Steps

If you’re still experiencing issues, follow these troubleshooting steps:

  1. Check Edge Version Compatibility

    • Ensure you’re using a supported version of Edge (Edge 80+ works best with Puppeteer Sharp)
    • Update Edge to the latest version
  2. Add More Logging

    csharp
    try
    {
        var browser = await Puppeteer.LaunchAsync(options);
        Console.WriteLine("Browser launched successfully");
        
        var page = await browser.NewPageAsync();
        Console.WriteLine("Page created successfully");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex.Message}");
        Console.WriteLine($"Stack trace: {ex.StackTrace}");
    }
    
  3. Test with Different URLs

    • Try navigating to simpler URLs first
    • Test with both HTTP and HTTPS URLs
  4. Check System Requirements

    • Ensure you have enough disk space for the browser
    • Verify you have the necessary system permissions
  5. Try Running with Elevated Permissions

    • Sometimes running as administrator helps, especially on systems with strict security policies

Best Practices for Puppeteer Sharp with Edge

When using Puppeteer Sharp with Microsoft Edge, follow these best practices:

  1. Always Specify Browser Arguments

    • Include --no-sandbox and --disable-gpu for better compatibility
    • Add --disable-devtools when not using developer tools
  2. Handle Timeouts Properly

    csharp
    var page = await browser.NewPageAsync();
    await page.SetDefaultTimeoutAsync(30000); // 30 seconds timeout
    
  3. Implement Proper Error Handling

    csharp
    try
    {
        await page.GoToAsync("https://example.com", new NavigationOptions { Timeout = 30000 });
    }
    catch (NavigationException ex)
    {
        Console.WriteLine($"Navigation failed: {ex.Message}");
        // Implement fallback logic
    }
    
  4. Use Headless Mode for Production

    • Headless mode is generally more stable and performs better
    • Only use headed mode for debugging purposes
  5. Keep Libraries Updated

    • Regularly update Puppeteer Sharp to the latest version
    • Keep your Edge browser updated

Conclusion

To resolve the issue where Puppeteer Sharp hangs when creating a new page with developer tools disabled:

  1. Modify your LaunchOptions to explicitly set Devtools = false
  2. Add the --disable-devtools argument to your browser launch options
  3. Consider additional arguments for better compatibility
  4. If issues persist, try running in headless mode or using the standalone Chromium version

The key is ensuring proper communication between your .NET code and the browser instance, which requires careful configuration of both Puppeteer Sharp and the browser launch parameters.

If you continue to experience issues, check your Edge browser version compatibility and consider implementing more robust error handling in your code to diagnose potential problems.