我正在尝试导航一个页面,然后我想访问一些页面元素,但我得到了我在下面提到的那些错误。我在下面分享了我的代码,你可以试试。我该如何解决这个问题?
Puppeteersharp 版本:2.0.3 操作系统:Windows 10 .Net 库:.Net Core 3.1
class Program
{
static async Task Main(string[] args)
{
try
{
var page = await GetPageInstance(GetOptions());
try
{
await page.GoToAsync("https://www.kijk.nl/", new NavigationOptions { WaitUntil = new WaitUntilNavigation[] { WaitUntilNavigation.Load, WaitUntilNavigation.Networkidle2 } });
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
var possibleCookieAcceptElements = await page.QuerySelectorAllAsync("button");
Console.Read();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static LaunchOptions GetOptions()
{
return new LaunchOptions
{
Headless = false,
Timeout = 0,
ExecutablePath = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
Args = new string[] {
"--no-sandbox",
"--disable-notifications",
"--disable-dev-shm-usage",
"--disable-setuid-sandbox",
"--lang=en-US,en-GB,en",
},
Devtools = false,
IgnoreHTTPSErrors = true,
DumpIO = false
};
}
public static async Task<Page> GetPageInstance(LaunchOptions options)
{
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
var browser = await Puppeteer.LaunchAsync(options);
var page = await browser.NewPageAsync();
return page;
}
}
我为内部 catch 块收到的错误消息:
导航失败,因为浏览器已断开连接!(NetworkManager 无法处理 Network.requestWillBeSent。值不能为空。(参数“键”)。在 System.Collections.Concurrent.ConcurrentDictionary2.ThrowKeyNullException() 在 System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory)在 PuppeteerSharp.Helpers.MultiMap2.Add(TKey key, TValue value) 在 PuppeteerSharp.Helpers.AsyncDictionaryHelper`2.GetItemAsync(TKey key) 在 PuppeteerSharp.NetworkManager.OnRequestAsync(RequestWillBeSentPayload e, String interceptionId) 在 PuppeteerSharp.NetworkManager.OnRequestWillBeSentAsync(RequestWillBeSentPayload e) 在 PuppeteerSharp.NetworkManager.Client_MessageReceived(Object sender, MessageEventArgs e))
我为外部 catch 块收到的错误消息:
协议错误(Runtime.evaluate):会话关闭。很可能页面已关闭。关闭原因:NetworkManager 无法处理 Network.requestWillBeSent。值不能为空。(参数“键”)。在 System.Collections.Concurrent.ConcurrentDictionary2.ThrowKeyNullException() 在 System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory) 在 PuppeteerSharp.Helpers.MultiMap2.Add(TKey key, TValue value) 在 PuppeteerSharp.Helpers.AsyncDictionaryHelper `2.GetItemAsync(TKey key) at PuppeteerSharp.NetworkManager.OnRequestAsync(RequestWillBeSentPayload e, String interceptionId) at PuppeteerSharp.NetworkManager.OnRequestWillBeSentAsync(RequestWillBeSentPayload e) at PuppeteerSharp.NetworkManager.Client_MessageReceived(Object sender, MessageEventArgs e)