1

I'm developing bot. He is working normally when headless mode is set to false. Whenever I start it with headless mode set to true, it throws timeout errors because it didn't find my selectors. I thought it would be maybe because of different resolution in both modes. So I set static default viewport. It fixed nothing.

Is it even possible to click with pupeeteer in headless mode? I would like to achieve that so I don't have multiple chromes open.

Creating browser

  _browser = await Puppeteer.LaunchAsync(new LaunchOptions
        {
            Headless = true,
            ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
            Args = new[] { "--disable-web-security", "--disable-infobars" },
            DefaultViewport = new ViewPortOptions { Height = 1080, Width = 1920},
           
        }) ;
        var pagesAsync = await _browser.PagesAsync();
        _page = pagesAsync.FirstOrDefault();


  const string logInButtom = "#__layout > div > nav > div.uinfo-wrapper.flex > div.login-btn-wrap > button";
        await _page.WaitForSelectorAsync(logInButtom);
        await _page.ClickAsync(logInButtom);        
        System.Threading.Thread.Sleep(1500);
        Debug.WriteLine("Succesfull login show");

Here is piece of code that works headless = false. Doesnt work headless = true

await _page.WaitForSelectorAsync(logInButtom); throws time out.

SPIRINN
  • 61
  • 6
  • What is the exact exception message and stacktrace? – ˈvɔlə Jun 14 '22 at 20:08
  • PuppeteerSharp.WaitTaskTimeoutException: 'waiting for selector 'body > div:nth-child(98) > div > div.content-box > div.content-left > div:nth-child(1) > div:nth-child(2) > div > input' failed: timeout 30000 ms exceeded' – SPIRINN Jun 14 '22 at 20:16
  • This selector works fine with headless mode off. But 1 selector worked now, but from here is always fails with headless on. I would do it via Xpath but I didn't find way to do it with pupeeteer – SPIRINN Jun 14 '22 at 20:18
  • Can you debug your code and check if the HTML really contains the element you want to select? Maybe you can create a html file and check if the selector works in a browser. – ˈvɔlə Jun 14 '22 at 20:34
  • I will probably go to selenium, I didn't have problems with it in other project. But I will give it a shot – SPIRINN Jun 14 '22 at 21:05
  • WTF :D same thing in selenium: 'no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="__layout"]/div/nav/div[3]/div[3]/button"} (Session info: headless chrome=102.0.5005.115)' – SPIRINN Jun 14 '22 at 21:41
  • The element is obviously missing or the selector is erroneous. Maybe the website delivers a different markup for search engines? Once again: Just debug the problem and validate the presence of the DOM element in the returned HTML. – ˈvɔlə Jun 15 '22 at 05:28
  • You could try to change the user agent. E.g.: `await _page.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");` – ˈvɔlə Jun 15 '22 at 05:29
  • 1
    I got it working in selenium, I again set static height and width and then it works in headless. But in puppeteer didnt. – SPIRINN Jun 15 '22 at 14:15

0 Answers0