How to use PressAsync method of PuppeteerSharp.ElementHandle class

Best Puppeteer-sharp code snippet using PuppeteerSharp.ElementHandle.PressAsync

ElementHandle.cs

Source:ElementHandle.cs Github

copy

Full Screen

...198 /// </summary>199 /// <param name="text">A text to type into a focused element</param>200 /// <param name="options">type options</param>201 /// <remarks>202 /// To press a special key, like <c>Control</c> or <c>ArrowDown</c> use <see cref="ElementHandle.PressAsync(string, PressOptions)"/>203 /// </remarks>204 /// <example>205 /// <code>206 /// elementHandle.TypeAsync("#mytextarea", "Hello"); // Types instantly207 /// elementHandle.TypeAsync("#mytextarea", "World", new TypeOptions { Delay = 100 }); // Types slower, like a user208 /// </code>209 /// An example of typing into a text field and then submitting the form:210 /// <code>211 /// var elementHandle = await page.GetElementAsync("input");212 /// await elementHandle.TypeAsync("some text");213 /// await elementHandle.PressAsync("Enter");214 /// </code>215 /// </example>216 /// <returns>Task</returns>217 public async Task TypeAsync(string text, TypeOptions options = null)218 {219 await FocusAsync().ConfigureAwait(false);220 await Page.Keyboard.TypeAsync(text, options).ConfigureAwait(false);221 }222 /// <summary>223 /// Focuses the element, and then uses <see cref="Keyboard.DownAsync(string, DownOptions)"/> and <see cref="Keyboard.UpAsync(string)"/>.224 /// </summary>225 /// <param name="key">Name of key to press, such as <c>ArrowLeft</c>. See <see cref="KeyDefinitions"/> for a list of all key names.</param>226 /// <param name="options">press options</param>227 /// <remarks>228 /// If <c>key</c> is a single character and no modifier keys besides <c>Shift</c> are being held down, a <c>keypress</c>/<c>input</c> event will also be generated. The <see cref="DownOptions.Text"/> option can be specified to force an input event to be generated.229 /// </remarks>230 /// <returns></returns>231 public async Task PressAsync(string key, PressOptions options = null)232 {233 await FocusAsync().ConfigureAwait(false);234 await Page.Keyboard.PressAsync(key, options).ConfigureAwait(false);235 }236 /// <summary>237 /// The method runs <c>element.querySelector</c> within the page. If no element matches the selector, the return value resolve to <c>null</c>.238 /// </summary>239 /// <param name="selector">A selector to query element for</param>240 /// <returns>Task which resolves to <see cref="ElementHandle"/> pointing to the frame element</returns>241 public async Task<ElementHandle> QuerySelectorAsync(string selector)242 {243 var handle = await ExecutionContext.EvaluateFunctionHandleAsync(244 "(element, selector) => element.querySelector(selector)",245 this, selector).ConfigureAwait(false);246 if (handle is ElementHandle element)247 {248 return element;...

Full Screen

Full Screen

VkBrwUserFactory.cs

Source:VkBrwUserFactory.cs Github

copy

Full Screen

...83 if (pagesVisibleElements.Length < pagesTotalCount)84 {85 PressOptions pressOptions = new PressOptions { Delay = new Random().Next(20, 40)};86 await userPage.FocusAsync("input");87 await userPage.Keyboard.PressAsync("Tab", pressOptions);88 int visiblePagesCounter = pagesVisibleElements.Length;89 while (visiblePagesCounter < pagesTotalCount)90 {91 await userPage.Keyboard.PressAsync("PageDown", pressOptions);92 await Task.Delay(new Random().Next(250, 350));93 await userPage.Keyboard.PressAsync("PageDown", pressOptions);94 await Task.Delay(new Random().Next(250, 350));95 await userPage.Keyboard.PressAsync("PageDown", pressOptions);96 await Task.Delay(new Random().Next(250, 350));97 await userPage.Keyboard.PressAsync("PageDown", pressOptions);98 await Task.Delay(new Random().Next(250, 350));99 ElementHandle[] newPagesVisibleElements = await userPage.QuerySelectorAllAsync("div.fans_idol_row.inl_bl");100 if (newPagesVisibleElements.Length == visiblePagesCounter)101 {102 break;103 }104 visiblePagesCounter = newPagesVisibleElements.Length;105 }106 }107 ElementHandle[] nwPagesElements = await userPage.QuerySelectorAllAsync("div.fans_idol_info");108 foreach (var element in nwPagesElements)109 {110 VkBrwCommunity community = await GetCommunityAsync(element, userContext);111 if (community != null)...

Full Screen

Full Screen

PuppeteerExtensions.cs

Source:PuppeteerExtensions.cs Github

copy

Full Screen

...195 while (true)196 {197 await page.FocusAsync(selector);198 await page.Keyboard.DownAsync("Control");199 await page.Keyboard.PressAsync("A");200 await page.Keyboard.UpAsync("Control");201 await page.Keyboard.PressAsync("Backspace");202 await page.Keyboard.TypeAsync(text, options);203 element = await page.QuerySelectorAsync(selector);204 if (element != null)205 {206 var value = await page.GetValueAsync(selector);207 if (value != text)208 continue;209 }210 return;211 }212 }213 /// <summary>214 /// Waits for a selector to hide215 /// </summary>...

Full Screen

Full Screen

Program.cs

Source:Program.cs Github

copy

Full Screen

...37 await TypeFieldValue(page, userNameSelector, faceBookEmail, delay);38 //password39 var passwordSelector = "#pass";40 await TypeFieldValue(page, passwordSelector, faceBookPassword, delay);41 await page.Keyboard.PressAsync("Tab");42 await page.Keyboard.PressAsync("Enter");43 //var SearchInputSelector = "label.rq0escxv.a8c37x1j.a5nuqjux.l9j0dhe7.k4urcfbm input";44 //await page.WaitForSelectorAsync(SearchInputSelector);45 //await TypeFieldValueSearch(page, SearchInputSelector, "JavaScript, React, and Node.js", delay);46 //await page.ClickAsync("div.oajrlxb2.gs1a9yip");47 //await page.GoToAsync("https://www.facebook.com/search/top/?q=JavaScript%2C%20React%2C%20and%20Node.js");48 Thread.Sleep(1000);49 await page.GoToAsync("https://www.facebook.com/groups/javascript.react.node/");50 Thread.Sleep(10000);51 Func<Task> scroll = null;52 scroll = new Func<Task>(async () =>53 {54 await page.Keyboard.DownAsync("ArrowDown");55 });56 for (int i = 0; i < 20; i++)57 {58 Thread.Sleep(300);59 Console.WriteLine("scroll");60 await scroll();61 }62 var jscode = @"() => {63 const selectors = Array.from(document.querySelectorAll(`div.du4w35lb.k4urcfbm.l9j0dhe7.sjgh65i0`)); 64 return selectors.map( t=> {return {content: t.innerHTML}});65 }";66 var results = await page.EvaluateFunctionAsync<Content[]>(jscode);67 List<Data> authorNamesAndText = new List<Data>();68 foreach (var result in results)69 {70 Data data = new Data();71 await page.SetContentAsync(result.content);72 Thread.Sleep(500);73 string content = "", content2 = "";74 var elementHandle = await page.QuerySelectorAsync("span.d2edcug0.hpfvmrgz.qv66sw1b.c1et5uql.lr9zc1uh.a8c37x1j.keod5gw0.nxhoafnm.aigsh9s9.d3f4x2em.fe6kdd0r.mau55g9w.c8b282yb.iv3no6db.jq4qci2q.a3bd9o3v.knj5qynh.m9osqain.hzawbc8m");75 if (elementHandle != null)76 {77 try78 {79 content = await elementHandle.QuerySelectorAsync("span").EvaluateFunctionAsync<string>("node => node.innerText");80 }81 catch (Exception e1)82 {83 Console.WriteLine("error in getting content: " + e1.Message);84 }85 }86 if (content != "")87 {88 Thread.Sleep(500);89 try90 {91 content2 = await page.QuerySelectorAsync("div.kvgmc6g5.cxmmr5t8.oygrvhab.hcukyx3x.c1et5uql.ii04i59q").EvaluateFunctionAsync<string>("node => node.innerText");92 }93 catch (Exception e2)94 {95 Console.WriteLine("error in getting content: " + e2.Message);96 }97 data.author = content;98 data.posttext = content2;99 authorNamesAndText.Add(data);100 }101 }102 //print author and post list103 foreach (Data ap in authorNamesAndText)104 {105 Console.WriteLine("Author: " + ap.author);106 Console.WriteLine("Post: " + ap.posttext);107 }108 await browser.CloseAsync();109 }110 private static void Page_Response(object sender, ResponseCreatedEventArgs e)111 {112 Console.WriteLine(e.Response.Status);113 }114 private static void Page_Request(object sender, RequestEventArgs e)115 {116 Console.WriteLine(e.Request.ResourceType.ToString());117 Console.WriteLine(e.Request.Url);118 }119 private static async Task TypeFieldValue(Page page, string fieldSelector, string value, int delay = 0)120 {121 await page.FocusAsync(fieldSelector);122 await page.TypeAsync(fieldSelector, value, new TypeOptions { Delay = delay });123 await page.Keyboard.PressAsync("Tab");124 }125 private static async Task TypeFieldValueSearch(Page page, string fieldSelector, string value, int delay = 0)126 {127 await page.FocusAsync(fieldSelector);128 await page.TypeAsync(fieldSelector, value, new TypeOptions { Delay = delay });129 }130 }131 public class Data132 {133 public string author { get; set; }134 public string posttext { get; set; }135 }136 public class Content137 {...

Full Screen

Full Screen

HunterSearch.cs

Source:HunterSearch.cs Github

copy

Full Screen

...31 {32 await page.GoToAsync($"https://hunter.ddosecrets.com/search", navigationOptions);33 //await page.WaitForNavigationAsync(navigationOptions);34 await page.TypeAsync("#search-box", query, new TypeOptions { Delay = 50 });35 await page.Keyboard.PressAsync($"{nameof(Key.Enter)}");36 //await page.WaitForTimeoutAsync(5000);37 var contentSelector = "a.EntityLink";38 await page.WaitForSelectorAsync(contentSelector);39 var elementHandles = await page.QuerySelectorAllAsync(contentSelector);40 var elementCount = elementHandles.Length;41 await page.ClickAsync("main.ContentPane");42 var elementCountPrev = 0;43 while (elementCount > elementCountPrev)44 {45 elementCountPrev = elementCount;46 await page.Keyboard.DownAsync($"{nameof(Key.Control)}");47 await page.Keyboard.PressAsync($"{nameof(Key.End)}");48 await page.Keyboard.UpAsync($"{nameof(Key.Control)}");49 await page.WaitForTimeoutAsync(1500);50 elementHandles = await page.QuerySelectorAllAsync(contentSelector);51 elementCount = elementHandles.Length;52 }53 Report($"query:{query} - found {elementCount} links");54 var downloadLinks = new List<string>(elementCount);55 foreach (var item in elementHandles)56 {57 var text = (await (await item.GetPropertyAsync("href")).JsonValueAsync()).ToString();58 downloadLinks.Add(text);59 }60 navigationOptions = new NavigationOptions { WaitUntil = new[] { WaitUntilNavigation.DOMContentLoaded } };61 foreach (var link in downloadLinks)...

Full Screen

Full Screen

PuppeteerSharpRepoSpecs.cs

Source:PuppeteerSharpRepoSpecs.cs Github

copy

Full Screen

...26 page.QuerySelectorAsync("h1").ShouldHaveContent("Where the world builds software");27 ElementHandle input = page.QuerySelectorAsync("input.header-search-input").Await();28 if (input.IsHidden()) page.ClickAsync(".octicon-three-bars").Await();29 page.TypeAsync("input.header-search-input", "Puppeteer Sharp").Await();30 page.Keyboard.PressAsync("Enter").Await();31 page.WaitForNavigationAsync().Await();32 ElementHandle[] repositories = page.QuerySelectorAllAsync(".repo-list-item").Await();33 repositories.Length.ShouldBeGreaterThan(0);34 var repository = repositories.First();35 repository.ShouldHaveContent("hardkoded/puppeteer-sharp");36 ElementHandle text = repository.QuerySelectorAsync("p").Await();37 text.ShouldHaveContent("Headless Chrome .NET API");38 ElementHandle link = repository.QuerySelectorAsync("a").Await();39 link.ClickAsync().Await();40 page.WaitForNavigationAsync().Await();41 page.QuerySelectorAsync("article > h1").ShouldHaveContent("Puppeteer Sharp");42 page.Url.ShouldEqual("https://github.com/hardkoded/puppeteer-sharp");43 };44 }...

Full Screen

Full Screen

PageObjects.cs

Source:PageObjects.cs Github

copy

Full Screen

...19 {20 var input = await SearchInput;21 if (await input.IsHiddenAsync()) await Page.ClickAsync(".octicon-three-bars");22 await input.TypeAsync(text);23 await input.PressAsync(Key.Enter);24 return await Page.WaitForNavigationAsync<GitHubSearchPage>();25 }26 }27 public class GitHubSearchPage : PageObject28 {29 [Selector(".repo-list-item")]30 public virtual Task<GitHubRepoListItem[]> RepoListItems { get; }31 }32 public class GitHubRepoListItem : ElementObject33 {34 [Selector("a")]35 public virtual Task<ElementHandle> Link { get; }36 [Selector("p")]37 public virtual Task<ElementHandle> Text { get; }...

Full Screen

Full Screen

PressOptions.cs

Source:PressOptions.cs Github

copy

Full Screen

2{3 /// <summary>4 /// options to use when pressing a key.5 /// </summary>6 /// <seealso cref="Keyboard.PressAsync(string, PressOptions)"/>7 /// <seealso cref="ElementHandle.PressAsync(string, PressOptions)"/>8 public class PressOptions : DownOptions9 {10 /// <summary>11 /// Time to wait between <c>keydown</c> and <c>keyup</c> in milliseconds. Defaults to 0.12 /// </summary>13 public int? Delay { get; set; }14 }...

Full Screen

Full Screen

PressAsync

Using AI Code Generation

copy

Full Screen

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 LaunchOptions { Headless = false }))10 using (var page = await browser.NewPageAsync())11 {12 var searchInput = await page.QuerySelectorAsync("input[name=q]");13 await searchInput.FocusAsync();14 await searchInput.PressAsync("a");15 await searchInput.PressAsync("b");16 await searchInput.PressAsync("c");17 await searchInput.PressAsync("Enter");18 await page.WaitForNavigationAsync();19 Console.WriteLine(page.Url);20 }21 }22 }23}

Full Screen

Full Screen

PressAsync

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static void Main(string[] args)7 {8 Run().Wait();9 }10 static async Task Run()11 {12 var browser = await Puppeteer.LaunchAsync(new LaunchOptions13 {14 });15 var page = await browser.NewPageAsync();16 var searchBox = await page.QuerySelectorAsync("input[name='q']");17 await searchBox.PressAsync("PuppeteerSharp");18 await searchBox.PressAsync("Enter");19 await page.WaitForNavigationAsync();20 await browser.CloseAsync();21 }22 }23}

Full Screen

Full Screen

PressAsync

Using AI Code Generation

copy

Full Screen

1var page = await browser.NewPageAsync();2var searchBox = await page.QuerySelectorAsync("input[name='q']");3await searchBox.TypeAsync("puppeteer-sharp");4var button = await page.QuerySelectorAsync("input[name='btnK']");5await button.PressAsync("Enter");6await page.WaitForNavigationAsync();7var page = await browser.NewPageAsync();8await page.Keyboard.PressAsync("puppeteer-sharp");9await page.Keyboard.PressAsync("Enter");10await page.WaitForNavigationAsync();11protected void Button1_Click(object sender, EventArgs e)12{13 var page = await browser.NewPageAsync();14 await page.Keyboard.PressAsync("puppeteer-sharp");15 await page.Keyboard.PressAsync("Enter");16 await page.WaitForNavigationAsync();17}18protected void Button1_Click(object sender, EventArgs e)19{20 var page = await browser.NewPageAsync();21 await page.Keyboard.PressAsync("puppeteer-sharp");22 await page.Keyboard.PressAsync("Enter");23 await page.WaitForNavigationAsync();24}

Full Screen

Full Screen

PressAsync

Using AI Code Generation

copy

Full Screen

1var page = await browser.NewPageAsync();2var searchBox = await page.QuerySelectorAsync("input[name=q]");3await searchBox.TypeAsync("Puppeteer");4await searchBox.PressAsync("Enter");5var page = await browser.NewPageAsync();6await page.TypeAsync("input[name=q]", "Puppeteer");7await page.PressAsync("input[name=q]", "Enter");8var page = await browser.NewPageAsync();9var searchBox = await page.QuerySelectorAsync("input[name=q]");10await searchBox.TypeAsync("Puppeteer");11await searchBox.PressAsync("Enter");12var page = await browser.NewPageAsync();13await page.TypeAsync("input[name=q]", "Puppeteer");14await page.PressAsync("input[name=q]", "Enter");15var page = await browser.NewPageAsync();16var searchBox = await page.QuerySelectorAsync("input[name=q]");17await searchBox.TypeAsync("Puppeteer");18await searchBox.PressAsync("Enter");19var page = await browser.NewPageAsync();20await page.TypeAsync("input[name=q]", "Puppeteer");21await page.PressAsync("input[name=q]", "Enter");22var page = await browser.NewPageAsync();23var searchBox = await page.QuerySelectorAsync("input[name=q]");24await searchBox.TypeAsync("Puppeteer");25await searchBox.PressAsync("Enter");26var page = await browser.NewPageAsync();27await page.GoToAsync("https

Full Screen

Full Screen

PressAsync

Using AI Code Generation

copy

Full Screen

1var page = await browser.NewPageAsync();2var input = await page.QuerySelectorAsync("input[name=q]");3await input.TypeAsync("puppeteer-sharp");4await input.PressAsync("Enter");5await page.WaitForNavigationAsync();6await page.ScreenshotAsync("google.png");7var page = await browser.NewPageAsync();8var input = await page.QuerySelectorAsync("input[name=q]");9await input.TypeAsync("puppeteer-sharp");10await page.Keyboard.PressAsync("Enter");11await page.WaitForNavigationAsync();12await page.ScreenshotAsync("google.png");

Full Screen

Full Screen

PressAsync

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static void Main(string[] args)7 {8 MainAsync().Wait();9 }10 static async Task MainAsync()11 {12 var browser = await Puppeteer.LaunchAsync(new LaunchOptions13 {14 ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"15 });16 var page = await browser.NewPageAsync();17 var searchButton = await page.QuerySelectorAsync("input[name='btnK']");18 await searchButton.PressAsync("Enter");19 await Task.Delay(5000);20 await browser.CloseAsync();21 }22 }23}24PuppeteerSharp.ElementHandle.PressAsync Method (String, PressOptions)25PuppeteerSharp.ElementHandle.PressAsync Method (String, PressOptions)26PuppeteerSharp.ElementHandle.PressAsync Method (String, PressOptions)27PuppeteerSharp.ElementHandle.PressAsync Method (String, PressOptions)28PuppeteerSharp.ElementHandle.PressAsync Method (String, PressOptions)29PuppeteerSharp.ElementHandle.PressAsync Method (String, PressOptions)30PuppeteerSharp.ElementHandle.PressAsync Method (String, PressOptions)

Full Screen

Full Screen

PressAsync

Using AI Code Generation

copy

Full Screen

1var page = await browser.NewPageAsync();2var search = await page.QuerySelectorAsync("input[name='q']");3await search.ClickAsync();4await search.PressAsync("Enter");5await page.ScreenshotAsync("search.png");6var page = await browser.NewPageAsync();7var search = await page.QuerySelectorAsync("input[name='q']");8await search.ClickAsync();9await page.PressAsync("Enter");10await page.ScreenshotAsync("search.png");11var page = await browser.NewPageAsync();12var search = await page.QuerySelectorAsync("input[name='q']");13await search.ClickAsync();14await page.MainFrame.PressAsync("Enter");15await page.ScreenshotAsync("search.png");16var page = await browser.NewPageAsync();17var search = await page.QuerySelectorAsync("input[name='q']");18await search.ClickAsync();19await page.PressAsync("Enter");20await page.ScreenshotAsync("search.png");21var page = await browser.NewPageAsync();22var search = await page.QuerySelectorAsync("input[name='q']");23await search.ClickAsync();24await page.MainFrame.PressAsync("Enter");25await page.ScreenshotAsync("search.png");26var page = await browser.NewPageAsync();27var search = await page.QuerySelectorAsync("input[name='q']");28await search.ClickAsync();29await page.MainFrame.PressAsync("Enter");

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful