hardkoded / puppeteer-sharp

Headless Chrome .NET API
https://www.puppeteersharp.com
MIT License
3.41k stars 443 forks source link

Timeout of 1000 ms exceeded trying to EvaluateFunctionAsync #2659

Open lofti198 opened 5 months ago

lofti198 commented 5 months ago

.NET 8 console app, PuppeteerSharp 17.0.0. Loading this webpage https://www.olx.uz/d/obyavlenie/novyy-teplyy-svitshot-ls-vaikiki-ID34mqR.html . Trying to evaluate js, however getting strange exception (js checked in dev console and is correct):

Protocol error(Runtime.callFunctionOn): Target closed. (Connection failed to process Network.requestWillBeSent. Timeout of 1000 ms exceeded. at PuppeteerSharp.Helpers.TaskHelper.WithTimeout[T](Task1 task, TimeSpan timeout, Func2 exceptionFactory) in /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/Helpers/TaskHelper.cs:line 180 at PuppeteerSharp.Cdp.FrameManager.<>cDisplayClass58_0.<b0>d.MoveNext() in /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/Cdp/FrameManager.cs:line 206)

my code below

using var browserFetcher = new BrowserFetcher();
await browserFetcher.DownloadAsync();

var launchOptions = new LaunchOptions
{
    // UserDataDir = "C:\\Users\\38097\\Documents\\1Projects\\Puppeteer tests\\cache",
    Headless = false,
    IgnoredDefaultArgs= new[] {"--disable-extensions"},
    SlowMo = 5,
    Args = new string[] { }

};

var browser = await Puppeteer.LaunchAsync(launchOptions);
try
{
    var page = await browser.NewPageAsync();

    // If proxy with authentication

 await page.SetViewportAsync(new ViewPortOptions
 {
     Width = 1920,
     Height = 1080
 });

 await page.GoToAsync("https://www.olx.uz/d/obyavlenie/novyy-teplyy-svitshot-ls-vaikiki-ID34mqR.html",
     new NavigationOptions
     {
         WaitUntil = new WaitUntilNavigation[]
         { WaitUntilNavigation.DOMContentLoaded},
         Timeout = timeoutSec * 1000
     });

     await page.EvaluateFunctionAsync(@"() =>{//alert('hello');

                                                            const buttons = document.querySelectorAll('button');
                                                            for (let button of buttons)
                                                            {

                        const span = button.querySelector('span');
                        if (span && span.textContent.trim() === 'Показать телефон')
                        {
                            console.log('button found, gonna scroll')
                            button.scrollIntoView();
button.click();
break;
                        }
                        else
                        {

                        }
                    }

     return true;
}");
kblok commented 5 months ago

Thank you for the report. I'm on vacation right now. I'll take a look at it on Monday!

lofti198 commented 5 months ago

Thank you have a great vacation! !UPDATE! just found that commenting SlowMo fixes issue. However it seems that using it is sometimes important to imitate real user behavior (for example loading websites with cloudflar):

var launchOptions = new LaunchOptions { Headless = false, IgnoredDefaultArgs= new[] {"--disable-extensions"}, // SlowMo = 5, Args = new string[] { }

};

kblok commented 5 months ago

That helps a lot!