hardkoded / puppeteer-sharp

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

Cant accesss Mouse or Keyboard from Page #2784

Closed Coolgatty closed 1 month ago

Coolgatty commented 1 month ago

'Page' does not contain a definition for 'Mouse' and no accessible extension method 'Mouse' accepting a first argument of type 'Page' could be found (are you missing a using directive or an assembly reference?)CS1061

I updated to latest version, and now i cant access page.Mouse or page.Keyboard

kblok commented 1 month ago

hmmm We have the property here.

Coolgatty commented 1 month ago

Am i doing something wrong here?


public async Task<(Browser browser, Page page)> StartScraping(bool throttled = false)
        {

            Browser browser;

            Page page;

            (int width, int height) = GetDefaultViewport();
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                // Optimizado para AWS Lambda
                var browserLauncher = new HeadlessChromiumPuppeteerLauncher(_loggerFactory);

                var linuxArgs = new[]
                {
                    "--autoplay-policy=user-gesture-required",
                    "--disable-background-networking",
                    "--disable-background-timer-throttling",
                    "--disable-backgrounding-occluded-windows",
                    "--disable-breakpad",
                    "--disable-client-side-phishing-detection",
                    "--disable-component-update",
                    "--disable-default-apps",
                    "--disable-dev-shm-usage",
                    "--disable-domain-reliability",
                    "--disable-extensions",
                    "--disable-features=AudioServiceOutOfProcess,IsolateOrigins,site-per-process",
                    "--disable-hang-monitor",
                    "--disable-ipc-flooding-protection",
                    "--disable-offer-store-unmasked-wallet-cards",
                    "--disable-popup-blocking",
                    "--disable-print-preview",
                    "--disable-prompt-on-repost",
                    "--disable-renderer-backgrounding",
                    "--disable-setuid-sandbox",
                    "--disable-speech-api",
                    "--disable-sync",
                    "--disable-web-security",
                    "--disk-cache-size=33554432",
                    "--hide-scrollbars",
                    "--ignore-gpu-blocklist",
                    "--metrics-recording-only",
                    "--mute-audio",
                    "--no-default-browser-check",
                    "--no-first-run",
                    "--no-pings",
                    "--no-sandbox",
                    "--no-zygote",
                    "--password-store=basic",
                    "--use-gl=swiftshader",
                    "--use-mock-keychain",
                    "--single-process",
                    "--incognito",
                    $"--window-size={width},{height}"
                };

                browser = (Browser)await browserLauncher.LaunchAsync(linuxArgs);
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                Console.WriteLine("Iniciando scraping en Windows...");
                bool isHeadless = _configuration.GetSection("Enviornment").Value != "CERT";

                var launchOptions = new LaunchOptions
                {
                    Headless = isHeadless,
                    Args = new[]
                    {
                        "--incognito",
                        $"--window-size={width},{height}"
                    }
                };

                string _executablePath = _configuration.GetSection("Scraping:Windows:ExecutablePath").Value;
                if (!string.IsNullOrWhiteSpace(_executablePath) && File.Exists(_executablePath))
                {
                    launchOptions.ExecutablePath = _executablePath;
                }
                else // Descarga los archivos necesarios para ejecutar el chromium en tiempo de ejecución
                {
                    var browserFetcher = new BrowserFetcher();
                    await browserFetcher.DownloadAsync();
                }

                browser = (Browser)await Puppeteer.LaunchAsync(launchOptions);
            }

This is how i instantiate the page and then i pass it around through functions. and i get this:

image

On this code:

            private static async Task ClickCoordinates(Page page, dynamic coordinates)
        {
            if (coordinates != null)
            {
                await Task.Delay(1000);
                await page.Mouse.ClickAsync((decimal)coordinates.x, (decimal)coordinates.y); // Ensure you're operating in the context of the main page
                Console.WriteLine("Clicked the element at coordinates (" + coordinates.x + ", " + coordinates.y + ")");
            }
            else
            {
                throw new ScrapingException("No se pudo obtener las coordenadas del elemento", true);
            }
        }
kblok commented 1 month ago

Can you use IPage instead of Page and IBrowser instead of Browser?

I think I can relax that on an upcoming version. But that should get you unblocked.