Best Puppeteer-sharp code snippet using PuppeteerSharp.Page.TypeAsync
MainWindow.xaml.cs
Source:MainWindow.xaml.cs
...5758 await page.Keyboard.PressAsync("Escape"); //idk what this does? but it does something5960 //do login61 await page.TypeAsync("#email", username.Text); //enter email62 await page.TypeAsync("#pass", password.Password); //enter pasword63 await page.ClickAsync("[name = login]"); // clicks the login btn646566 await page.WaitForNavigationAsync(); //wait for newsfeed to load67686970 //idk wtf this is for ._. but hey i trust the judgement of past me71 await page.Keyboard.PressAsync("Escape"); //hits esc button 72 await page.Keyboard.PressAsync("End"); //hits end button737475 //starts an endless loop that keeps running this code76 while (true)77 {78 try79 {80 //get a list of all the posts on the newsfeed81 var posts = await page.QuerySelectorAllAsync("[data-visualcompletion='ignore-late-mutation'"); //get all posts 8283 for (int i = 0; i < posts.Length; i++) //go through the list of posts84 {85 //run js that gets the name of the person who made the post86 string js = $@"var n = document.querySelectorAll(""[data-visualcompletion='ignore-late-mutation'""); n[{i}].querySelectorAll('.qzhwtbm6.knvmm38d')[{0}].innerText;"; //get username string87 var WhoMadePost = await page.EvaluateExpressionAsync(js); //get username of everyone who made a post888990 Console.WriteLine(WhoMadePost.ToString()); //output the name of the person who made the post to console 9192 //check to see if the person who made the post is the target93 if (WhoMadePost.ToString() == Target.Text) 94 {95 //get all comments96 var comments = await posts[i].QuerySelectorAsync(".oo9gr5id.lzcic4wl.jm1wdb64.l9j0dhe7.gsox5hk5.mdldhsdk.ii04i59q.notranslate"); 97 Newtonsoft.Json.Linq.JToken check = null;9899 try100 {101 //check to see if already commented102 check = await page.EvaluateExpressionAsync($@"var n = document.querySelectorAll(""[data-visualcompletion = 'ignore-late-mutation'""); n[{i}].querySelectorAll('.cwj9ozl2.tvmbv18p')[0].innerText;");103 }104 catch105 {106 //i think this is to handle null errors??? idk why it does it 107 }108109 //if not commented run the code below110 if (check == null) 111 {112 await comments.TypeAsync(Comment.Text); //type comment113 await page.Keyboard.PressAsync("Enter"); //submit comment114 }115 else if (!check.ToString().Contains(FBusername.Text)) //im not sure what this check is for. all i know is the code works.116 {117 //check to see if already commented118 check = await page.EvaluateExpressionAsync($@"var n = document.querySelectorAll(""[data-visualcompletion = 'ignore-late-mutation'""); n[{i}].querySelectorAll('.cwj9ozl2.tvmbv18p')[0].innerText;");119120 if (!check.ToString().Contains(FBusername.Text))121 {122 await comments.TypeAsync(Comment.Text); //type comment123 await page.Keyboard.PressAsync("Enter"); //submit comment124 }125 }126 }127 }128 }129 catch130 {131132 }133 }134 }135136
...
AccountsControllerIntegrationTests.cs
Source:AccountsControllerIntegrationTests.cs
...61 await page.GoToAsync($"http://{_webHostFixture.Host}/test-client/index.html");62 var navigationTask = page.WaitForNavigationAsync();63 await Task.WhenAll(navigationTask, page.ClickAsync("button"));64 // 4. Fill out the login form65 await page.TypeAsync("#Username", _signupRequests[1].Email);66 await page.TypeAsync("#Password", _signupRequests[1].Password);67 // 5. Hit the login button and wait for redirect navigation...68 navigationTask = page.WaitForNavigationAsync(new NavigationOptions { WaitUntil = new[] { WaitUntilNavigation.Networkidle0 } });69 await Task.WhenAll(navigationTask, page.ClickAsync(".btn-primary"));70 var content = await page.GetContentAsync();71 await page.CloseAsync();72 // 6. Assert we have a logged-in state in the test client73 Assert.Contains("User logged in", content);74 Assert.Contains("Prescott Terrell", content);75 Assert.Contains("pterrell@mailinator.com", content);76 Assert.Contains("employer", content);77 }78 }79 }80 }...
TimeTrackingController.cs
Source:TimeTrackingController.cs
...31 return;32 }33 34 await page.GoToAsync(@"https://timetracker.bairesdev.com/");35 await page.TypeAsync("#ctl00_ContentPlaceHolder_UserNameTextBox", tracking.User);36 await page.TypeAsync("#ctl00_ContentPlaceHolder_PasswordTextBox", tracking.Password);37 await page.ClickAsync("#ctl00_ContentPlaceHolder_LoginButton");38 await page.GoToAsync(@"https://timetracker.bairesdev.com/CargaTimeTracker.aspx");39 40 await page.EvaluateExpressionAsync($@"document.querySelector('#ctl00_ContentPlaceHolder_txtFrom').value = null");41 await page.TypeAsync("#ctl00_ContentPlaceHolder_txtFrom", tracking.Date, new TypeOptions{ Delay = 30 });42 var dropProject = await page.QuerySelectorAsync("#ctl00_ContentPlaceHolder_idProyectoDropDownList");43 await dropProject.ClickAsync();44 45 var children = await dropProject.QuerySelectorAllAsync("option");46 await children[2].ClickAsync();47 48 await page.ClickAsync("#ctl00_ContentPlaceHolder_TiempoTextBox");49 await page.TypeAsync("#ctl00_ContentPlaceHolder_TiempoTextBox", tracking.Hours.ToString(), new TypeOptions{ Delay = 30 });50 await page.ClickAsync("#ctl00_ContentPlaceHolder_idTipoAsignacionDropDownList");51 //await page.FocusAsync("#ctl00_ContentPlaceHolder_idTipoAsignacionDropDownList");52 //await page.SelectAsync("#ctl00_ContentPlaceHolder_idTipoAsignacionDropDownList", tracking.AssignmentType);53 await page.ClickAsync("#ctl00_ContentPlaceHolder_DescripcionTextBox");54 await page.TypeAsync("#ctl00_ContentPlaceHolder_DescripcionTextBox", tracking.Description, new TypeOptions{ Delay = 30 });55 await page.FocusAsync("#ctl00_ContentPlaceHolder_idFocalPointClientDropDownList");56 await page.SelectAsync("#ctl00_ContentPlaceHolder_idFocalPointClientDropDownList", tracking.FocalPoint);57 await browser.CloseAsync();58 }).ConfigureAwait(false);59 }60 }61 62 public class Tracking63 {64 public string User { get; set; }65 66 public string Password { get; set; }67 68 public string Date { get; set; }...
Clean163Email.cs
Source:Clean163Email.cs
...20 var frame = page.Frames.First(s=>s.Name.Contains("x-URS-iframe"));21 22 //çå¾
æå·¥æä½23 var user = await frame.WaitForSelectorAsync("input[data-placeholder='é®ç®±å¸å·æææºå·ç ']");24 await user.TypeAsync("");25 var password = await frame.WaitForSelectorAsync("input[data-placeholder='è¾å
¥å¯ç ']");26 await password.TypeAsync("");27 await frame.ClickAsync("#dologin");28 var element = await page.WaitForXPathAsync("//*[a='æ¸
çé®ç®±']");29 var cleanBtn = await element.XPathAsync("a[1]");30 await cleanBtn[0].ClickAsync();31 await Task.Delay(3000);32 var frame2 = page.Frames.First(s => s.Name.Contains("frmoutlink"));33 34 await frame2.ClickAsync("#clearTypeDate");35 36 await frame2.ClickAsync("#dateCleanCustom");37 38 await frame2.TypeAsync("#customYearStartIpt", "1990");39 await frame2.TypeAsync("#customMonthStartIpt", "1");40 await frame2.TypeAsync("#customDayStartIpt", "1");41 await frame2.TypeAsync("#customYearEndIpt", "2021");42 await frame2.TypeAsync("#customMonthEndIpt", "2");43 await frame2.TypeAsync("#customDayEndIpt", "18");44 var wait = new WaitForSelectorOptions {Timeout = 5000};45 for (int i = 0; i < 100000; i++)46 {47 try48 {49 var b1 = await frame2.WaitForXPathAsync("//*/div[span='å¼å§æ«æ']", wait);50 await b1.ClickAsync();51 await Task.Delay(5000);52 var deleteBtn = await frame2.WaitForXPathAsync("//div[span='å½»åºå é¤']", wait);53 await deleteBtn.ClickAsync();54 var confirmBtn = await page.WaitForXPathAsync("//div[span='ç¡® å®']", wait);55 await confirmBtn.ClickAsync();56 await Task.Delay(4000);57 var confirm2Btn = await page.WaitForXPathAsync("//div[span='ç¡® å®']", wait);...
AuthIntegrationTests.cs
Source:AuthIntegrationTests.cs
...36 var page = await browser.NewPageAsync();37 await page.GoToAsync($"{app.RootUri}signup");38 {39 var emailField = await page.MainFrame.QuerySelectorAsync("#Email");40 await emailField.TypeAsync(Email);41 var passwordField = await page.MainFrame.QuerySelectorAsync("#Password");42 await passwordField.TypeAsync(Password);43 var confirmPasswordField = await page.MainFrame.QuerySelectorAsync("#ConfirmPassword");44 await confirmPasswordField.TypeAsync(Password);45 var submitButton = await page.MainFrame.QuerySelectorAsync("input[type=submit]");46 await submitButton.ClickAsync();47 await page.MainFrame.WaitForNavigationAsync();48 }49 {50 using var scope = app.Services.CreateScope();51 var dbContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>();52 var query = from user in dbContext.Users.AsQueryable() where user.Email == Email select user;53 var exists = await query.AnyAsync();54 exists.Should().Be(true);55 }56 {57 var cookies = await page.GetCookiesAsync();58 await page.DeleteCookieAsync(cookies);59 await page.GoToAsync($"{app.RootUri}login", waitUntil: WaitUntilNavigation.DOMContentLoaded);60 }61 {62 var emailField = await page.MainFrame.QuerySelectorAsync("#Email");63 await emailField.TypeAsync(Email);64 var passwordField = await page.MainFrame.QuerySelectorAsync("#Password");65 await passwordField.TypeAsync(Password);66 var submitButton = await page.MainFrame.QuerySelectorAsync("input[type=submit]");67 await submitButton.ClickAsync();68 await page.MainFrame.WaitForNavigationAsync();69 var url = new Uri(page.Url);70 url.AbsolutePath.Should().Be("/");71 }72 }73 }74}...
KeyboardTypeFunction.cs
Source:KeyboardTypeFunction.cs
...47 {48 var element = await page.QuerySelectorByXPath(selector);49 if (element != null)50 {51 await element.TypeAsync(text, typeOptions);52 }53 else54 {55 throw new Exception($"Node not found with '{selector}' selector on type function");56 }57 }58 else59 {60 await page.TypeAsync(selector, text, typeOptions); 61 }62 }63 else64 {65 var currentFrame = page.Frames.FirstOrDefault(x => x.Name == frame);66 if (currentFrame != null)67 {68 if (selector.StartsWith(XPathSelector.XPathSelectorToken))69 {70 var element = await currentFrame.QuerySelectorByXPath(selector);71 if (element != null)72 {73 await element.TypeAsync(text, typeOptions);74 }75 else76 {77 throw new Exception($"Node not found with '{selector}' selector on type function");78 }79 }80 else81 {82 await currentFrame.TypeAsync(selector, text, typeOptions); 83 }84 }85 else86 {87 throw new Exception($"Frame not found with name '{frame}'");88 }89 }90 }91 #endregion92 }93}...
Program.cs
Source:Program.cs
...17 18 using var page = await browser.NewPageAsync();19 await Task.Delay(8000);20 await page.GoToAsync("https://localhost:5001/Identity/Account/Login");21 await page.TypeAsync("#Input_Email", "severino.padovano@example.com");22 await page.TypeAsync("#Input_Password", "Teacher1!");23 await Task.Delay(3000);24 var navigationTask = page.WaitForNavigationAsync();25 await page.ClickAsync(".container button[type=submit]");26 await navigationTask;27 for (var i = 1; i < 32; i++)28 {29 await page.GoToAsync($"https://localhost:5001/Contact?id={i}");30 await page.TypeAsync("#Question", "Lorem ipsum dolor sit amet");31 await Task.Delay(1000);32 navigationTask = page.WaitForNavigationAsync();33 await page.ClickAsync(".container button[type=submit]");34 await navigationTask;35 await Task.Delay(1000);36 }37 Console.ReadLine();38 }39 }40}...
LoginPage.cs
Source:LoginPage.cs
...8 public static async Task SingIn(Page page, string login, string password)9 {10 await page.WaitForSelectorAsync("input[name='username']");11 await page.ClickAsync("input[name='username']");12 await page.Keyboard.TypeAsync(login);13 await page.WaitForSelectorAsync("input[name='password']");14 await page.ClickAsync("input[name='password']");15 await page.Keyboard.TypeAsync(password);16 await page.WaitForSelectorAsync("._0mzm-.sqdOP.L3NKy");17 await page.ClickAsync("._0mzm-.sqdOP.L3NKy");18 }19 public static async Task<Page> WaitUntilLoginCompleted(Browser browser)20 {21 while (true)22 {23 foreach (var page in await browser.PagesAsync())24 {25 if (!page.Url.Contains("instagram")) return page;26 }27 await Task.Delay(1000);28 }29 }...
TypeAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static void Main(string[] args)7 {8 Console.WriteLine("Hello World!");9 MainAsync().GetAwaiter().GetResult();10 }11 static async Task MainAsync()12 {13 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false }))14 {15 var page = await browser.NewPageAsync();16 await page.TypeAsync("[name=q]", "PuppeteerSharp");17 await page.Keyboard.PressAsync("Enter");18 await page.ScreenshotAsync("screenshot.png");19 }20 }21 }22}
TypeAsync
Using AI Code Generation
1using System;2using PuppeteerSharp;3using System.Threading.Tasks;4{5 {6 static void Main(string[] args)7 {8 RunAsync().Wait();9 }10 static async Task RunAsync()11 {12 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false }))13 {14 var page = await browser.NewPageAsync();15 await page.TypeAsync("input[title='Search']", "Hello World!");16 }17 }18 }19}
TypeAsync
Using AI Code Generation
1await page.TypeAsync("input#name", "John Doe");2await frame.TypeAsync("input#name", "John Doe");3await elementHandle.TypeAsync("John Doe");4await jSHandle.TypeAsync("John Doe");5await cDPSession.TypeAsync("input#name", "John Doe");6await connection.TypeAsync("input#name", "John Doe");7await target.TypeAsync("input#name", "John Doe");8await page.TypeAsync("input#name", "John Doe");9await frame.TypeAsync("input#name", "John Doe");10await elementHandle.TypeAsync("John Doe");11await jSHandle.TypeAsync("John Doe");12await cDPSession.TypeAsync("input#name", "John Doe");13await connection.TypeAsync("input#name", "John Doe");14await target.TypeAsync("input#name", "John Doe");15await page.TypeAsync("input#name", "John Doe");
TypeAsync
Using AI Code Generation
1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5 {6 static void Main(string[] args)7 {8 Console.WriteLine("Hello World!");9 var task = RunAsync();10 task.Wait();11 }12 static async Task RunAsync()13 {14 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });15 var page = await browser.NewPageAsync();16 await page.TypeAsync("input[title='Search']", "PuppeteerSharp");17 await page.PressAsync("input[title='Search']", "Enter");18 await page.ScreenshotAsync("example.png");19 await browser.CloseAsync();20 }21 }22}23using PuppeteerSharp;24using System;25using System.Threading.Tasks;26{27 {28 static void Main(string[] args)29 {30 Console.WriteLine("Hello World!");31 var task = RunAsync();32 task.Wait();33 }34 static async Task RunAsync()35 {36 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });37 var page = await browser.NewPageAsync();38 await page.TypeAsync("input[title='Search']", "PuppeteerSharp");39 await page.ClickAsync("input[value='Google Search']");40 await page.ScreenshotAsync("example.png");41 await browser.CloseAsync();42 }43 }44}45using PuppeteerSharp;46using System;47using System.Threading.Tasks;48{49 {50 static void Main(string[] args)51 {52 Console.WriteLine("Hello World!");53 var task = RunAsync();
TypeAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static void Main(string[] args)7 {8 Console.WriteLine("Hello World!");9 new Program().Run().GetAwaiter().GetResult();10 }11 public async Task Run()12 {13 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);14 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false }))15 using (var page = await browser.NewPageAsync())16 {17 await page.TypeAsync("input[title='Search']", "PuppeteerSharp", new TypeOptions { Delay = 100 });18 await page.PressAsync("input[title='Search']", "Enter", new PressOptions { Delay = 100 });19 await page.ScreenshotAsync("example.png");20 }21 }22 }23}
TypeAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 {9 };10 var browser = await Puppeteer.LaunchAsync(options);11 var page = await browser.NewPageAsync();12 var task = page.TypeAsync("input[title='Search']", "PuppeteerSharp");13 await page.WaitForSelectorAsync("input[value='Google Search']");14 var input = await page.QuerySelectorAsync("input[value='Google Search']");15 await input.ClickAsync();16 await task;17 Console.WriteLine("Task Completed");18 Console.ReadLine();19 }20 }21}
TypeAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);9 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions10 {11 }))12 {13 var page = await browser.NewPageAsync();14 await requestTask;15 Console.WriteLine("Request made");16 }17 }18 }19}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!