首页 > 解决方案 > PuppeteerSharp 无法创建连接

问题描述

    {
  "Message": "An error has occurred.",
  "ExceptionMessage": "Failed to create connection",
  "ExceptionType": "PuppeteerSharp.ProcessException",
  "StackTrace": "   at PuppeteerSharp.Launcher.<LaunchAsync>d__8.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at PuppeteerSharp.Launcher.<LaunchAsync>d__8.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Vixi.Api.Helpers.TiktokPuppeeter.<CreatePage>d__5.MoveNext() in C:\\Program Files (x86)\\Jenkins\\workspace\\Vixi STAGE\\Vixi.Api\\Helpers\\TiktokPuppeeter.cs:line 73\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Vixi.Api.Helpers.TiktokPuppeeter.<Init>d__4.MoveNext() in C:\\Program Files (x86)\\Jenkins\\workspace\\Vixi STAGE\\Vixi.Api\\Helpers\\TiktokPuppeeter.cs:line 38\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Vixi.Api.Controllers.PluginController.<GetTiktokVidurl>d__2.MoveNext() in C:\\Program Files (x86)\\Jenkins\\workspace\\Vixi STAGE\\Vixi.Api\\Controllers\\PluginController.cs:line 141\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__1`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()",
  "InnerException": {
    "Message": "An error has occurred.",
    "ExceptionMessage": "Timeout of 30000 ms exceeded",
    "ExceptionType": "System.TimeoutException",
    "StackTrace": "   at PuppeteerSharp.Helpers.TaskHelper.<WithTimeout>d__8`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at PuppeteerSharp.Browser.<WaitForTargetAsync>d__67.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at PuppeteerSharp.Launcher.<LaunchAsync>d__8.MoveNext()"
  }
}

我已经添加了超时或设置为零,但它不起作用,它只会永远加载,甚至在后端创建大量 chromium 实例,以 100% 消耗 CPU 使用率。

static async Task<Page> CreatePage()
        {
          
            var browser = await Puppeteer.LaunchAsync(new LaunchOptions
            {
                Headless = false,
                ExecutablePath = ChromiumPath()
            });

            var page = await browser.NewPageAsync();

            return page;
        }

public static async Task<string> Init(string vidurl)
        {
            string vidUrl = "";

            using (var page = await CreatePage())
            {
                using (page.Browser)
                {
                    await page.WaitForTimeoutAsync(60000);
                    await page.GoToAsync(serviceUrl);
                    var input = await page.QuerySelectorAsync("#link_url");
                    await input.TypeAsync(vidurl);
                    var donwloadBtn = await page.QuerySelectorAsync(".btn");
                    await donwloadBtn.ClickAsync();

                    await page.WaitForNavigationAsync(new NavigationOptions { Timeout = 60000 });
                   // await page.WaitForNavigationAsync(new NavigationOptions { Timeout = 0 });

                    var jsSelectAllAnchors = @"Array.from(document.querySelectorAll('a')).map(a => a.href);";
                    var urls = await page.EvaluateExpressionAsync<string[]>(jsSelectAllAnchors);
                    foreach (string url in urls)
                    {
                        if (url.Contains("tiktokcdn.com"))
                        {
                            vidUrl = url;
                            break;
                        }
                    }
                    page.Dispose();
                    await page.Browser.CloseAsync();
                }

                return vidUrl;
            }
        }

标签: c#puppeteerconnection-timeout

解决方案


推荐阅读