C
C#4mo ago
AntiDev

✅ I cant start playwright browser

I cannot run the browser on Playwright, my code closes without throwing an error. code https://hatebin.com/rovbvubawd
No description
7 Replies
SinFluxx
SinFluxx4mo ago
I think it's probably related to you having a non-async Main calling an async method e.g.:
using Microsoft.Playwright;
using System.Diagnostics;

namespace ConsoleApp1
{
internal class Program
{
static async Task Main()
{
await Test();
}

static async Task Test()
{
Console.WriteLine("Started");

using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Firefox.LaunchAsync(new() { Headless = false });
var page = await browser.NewPageAsync();

await page.GotoAsync("https://ipv4.icanhazip.com/");
await page.ScreenshotAsync(new() { Path = "screenshot.png" });

Process.Start("screenshot.png");
Console.WriteLine("Finished");
}
}
}
using Microsoft.Playwright;
using System.Diagnostics;

namespace ConsoleApp1
{
internal class Program
{
static async Task Main()
{
await Test();
}

static async Task Test()
{
Console.WriteLine("Started");

using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Firefox.LaunchAsync(new() { Headless = false });
var page = await browser.NewPageAsync();

await page.GotoAsync("https://ipv4.icanhazip.com/");
await page.ScreenshotAsync(new() { Path = "screenshot.png" });

Process.Start("screenshot.png");
Console.WriteLine("Finished");
}
}
}
and see if it runs
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
AntiDev
AntiDev4mo ago
But Console.writeline at the end not working that worked thx i dont know main function can be a task
SinFluxx
SinFluxx4mo ago
$mains
MODiX
MODiX4mo ago
The possible signatures for Main are
public static void Main() { }
public static int Main() { }
public static void Main(string[] args) { }
public static int Main(string[] args) { }
public static async Task Main() { }
public static async Task<int> Main() { }
public static async Task Main(string[] args) { }
public static async Task<int> Main(string[] args) { }
public static void Main() { }
public static int Main() { }
public static void Main(string[] args) { }
public static int Main(string[] args) { }
public static async Task Main() { }
public static async Task<int> Main() { }
public static async Task Main(string[] args) { }
public static async Task<int> Main(string[] args) { }
public is not required (can be any accessibility). Top-level statements are compiled into a Main method and will use an appropriate signature depending on the body. https://docs.microsoft.com/en-US/dotnet/csharp/fundamentals/program-structure/main-command-line
Main() and command-line arguments - C#
Learn about Main() and command-line arguments. The 'Main' method is the entry point of an executable program.
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX4mo ago
TLDR on async/await: * every .net API that is suffixed with Async (eg: .Read() and .ReadAsync()) => use the Async version * if the API name ends with Async => await it * if the API returns a Task => await it * if you have to await in a method: * that method must have the async keyword (you could even suffix your function name with Async, up to you) * if it was returning T (eg:string) => it should now return Task<T> (eg: Task<string>) * APIs to ban and associated fix:
var r = t.Result; /* ===> */ var r = await t;
t.Wait(); /* ===> */ await t;
var r = t.GetAwaiter().GetResult(); /* ===> */ var r = await t;
Task.WaitAll(t1, t2); /* ===> */ await Task.WhenAll(t1, t2);
var r = t.Result; /* ===> */ var r = await t;
t.Wait(); /* ===> */ await t;
var r = t.GetAwaiter().GetResult(); /* ===> */ var r = await t;
Task.WaitAll(t1, t2); /* ===> */ await Task.WhenAll(t1, t2);