Forked from Davidaredding/Puppeteersharp with scrolling
Created
June 22, 2020 09:00
-
-
Save febritecno/0ed9c7a088b8d7f16f1b7e64ea48f7b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using PuppeteerSharp; | |
using System.Collections.Generic; | |
namespace netDemo | |
{ | |
public class Program | |
{ | |
const string Username = "USERNAME"; | |
const string Password = "PASSWORD"; | |
static async Task Main(string[] args) | |
{ | |
Console.WriteLine("Downloading Chrome"); | |
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision); | |
Console.WriteLine("Chrome is Current"); | |
var b = await Puppeteer.LaunchAsync(new LaunchOptions{ | |
Headless = false, | |
Args = new[]{"--no-sandbox --disable-notifications"} | |
}); | |
var p = await b.NewPageAsync(); | |
await p.Client.SendAsync("Emulation.clearDeviceMetricsOverride"); | |
await p.GoToAsync("Https://www.reddit.com/login"); | |
await p.WaitForSelectorAsync("fieldset.login"); | |
await p.ClickAsync("fieldset.login"); | |
await p.FocusAsync("fieldset.login :first-child"); | |
await p.Keyboard.TypeAsync(Username,new PuppeteerSharp.Input.TypeOptions{Delay=100}); | |
await p.FocusAsync("fieldset.password :first-child"); | |
await p.Keyboard.TypeAsync(Password,new PuppeteerSharp.Input.TypeOptions{Delay=100}); | |
await p.ClickAsync("button[type=\"submit\""); | |
await p.WaitForNavigationAsync(); | |
await p.GoToAsync("Https://www.reddit.com/r/pics"); | |
await p.WaitForSelectorAsync("HEAD"); | |
Console.WriteLine("Preparing to scroll"); | |
Func<Task> scroll = null; | |
scroll = new Func<Task>(async()=>{ | |
Console.WriteLine("Scrolling"); | |
await p.EvaluateExpressionAsync("window.scrollBy({top:10,behavior:'smooth'})"); | |
Thread.Sleep(100); | |
await scroll(); | |
}); | |
await scroll(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment