Best Puppeteer-sharp code snippet using PuppeteerSharp.Input.ClickOptions
PageServer.cs
Source:PageServer.cs
...183 //Console.WriteLine(aa);184 await page.WaitForTimeoutAsync(210);185 //await page.ClickAsync("button[report-eventid='MLoginRegister_SMSReceiveCode']");186 //await page.ClickAsync("button[report-eventid='MLoginRegister_SMSReceiveCode']");187 await page.ClickAsync("button[report-eventid='MLoginRegister_SMSReceiveCode']", new PuppeteerSharp.Input.ClickOptions { ClickCount = 3 });188 await page.ClickAsync("input[type=checkbox]");189 ///å»é¼çå¾
代ç 190 await WaitSendSms(page);191 string js = "document.body.outerText";192 var pageouterText = await page.EvaluateExpressionAsync(js);193 var pagetext = pageouterText.ToString();194 //Console.WriteLine(pagetext);195 var ckcount = 0;196 var tabcount = GetTableCount();197 if (_mainConfig.UPTYPE == UpTypeEum.ql)198 {199 var data = await getCount(qlkey);200 ckcount = data.ckcount;201 }202 203 System.Timers.Timer timer = new System.Timers.Timer(60000 * 3);204 timer.Elapsed += new System.Timers.ElapsedEventHandler(async (s, e) =>205 {206 Console.WriteLine(" ææºï¼" + Phone + " tabe èªå¨åæ¶ æ¶é´:" + DateTime.Now.ToString());207 Delpage(Phone, page);208 await page.CloseAsync();209 var oldpage = GetPage();210 if (oldpage == null)211 {212 await browser.CloseAsync();213 }214 timer.Dispose();215 });216 timer.AutoReset = false;217 if (pagetext.Contains("å®å
¨éªè¯") && !pagetext.Contains("éªè¯æå"))218 {219 Console.WriteLine(" ææºï¼" + Phone + " tabe å建 æ¶é´:" + DateTime.Now.ToString());220 timer.Start();221 Console.WriteLine(Phone + "å®å
¨éªè¯");222 // await PageClose(Phone);223 result.data = new { Status = 666, ckcount = ckcount, tabcount = tabcount };224 result.message = "åºç°å®å
¨éªè¯,";225 return result;226 }227 if (pagetext.Contains("çä¿¡å·²ç»åéï¼è¯·å¿éå¤æ交"))228 {229 await PageClose(Phone);230 result.data = new { Status = 505, pagetext = pagetext };231 result.message = "请å·æ°é¡µé¢éæ°ç»éã";232 return result;233 }234 if (pagetext.Contains("çä¿¡éªè¯ç åé次æ°å·²è¾¾ä¸é"))235 {236 await PageClose(Phone);237 result.data = new { Status = 505, pagetext = pagetext };238 result.message = "对ä¸èµ·ï¼çä¿¡éªè¯ç åé次æ°å·²è¾¾ä¸éï¼è¯·24å°æ¶ååè¯ã";239 return result;240 }241 if (pagetext.Contains("该ææºå·æªæ³¨åï¼å°ä¸ºæ¨ç´æ¥æ³¨åã"))242 {243 await PageClose(Phone);244 result.data = new { Status = 501 };245 result.message = "该ææºå·æªæ³¨å";246 return result;247 }248 Console.WriteLine(" ææºï¼" + Phone + " tabe å建 æ¶é´:" + DateTime.Now.ToString());249 timer.Start();250 if (pagetext.Contains("éæ°è·å"))251 {252 result.success = true;253 Console.WriteLine(Phone + "è·åéªè¯ç æå");254 }255 result.data = new { ckcount = ckcount, tabcount = tabcount };256 return result;257 }258 /// <summary>259 /// ç½ç»é®é¢æ以è¦åè¿ç§å»é¼çå¾
带代ç 260 /// </summary>261 /// <param name="page"></param>262 /// <returns></returns>263 public async Task<bool> WaitSendSms(Page page)264 {265 await page.WaitForTimeoutAsync(1000);266 string js = "document.body.outerText";267 var pageouterText = await page.EvaluateExpressionAsync(js);268 var pagetext = pageouterText.ToString();269 if (pagetext.Contains("å®å
¨éªè¯") || pagetext.Contains("çä¿¡å·²ç»åéï¼è¯·å¿éå¤æ交") || pagetext.Contains("çä¿¡éªè¯ç åé次æ°å·²è¾¾ä¸é") || pagetext.Contains("该ææºå·æªæ³¨åï¼å°ä¸ºæ¨ç´æ¥æ³¨åã") || pagetext.Contains("éæ°è·å"))270 {271 return true;272 }273 else274 {275 return await WaitSendSms(page);276 }277 }278 public async Task PageClose(string Phone)279 {280 var page = GetPage(Phone);281 if (page != null)282 {283 Delpage(Phone, page);284 var browser = page.Browser;285 await page.CloseAsync();286 var oldpage = GetPage();287 if (oldpage == null)288 {289 await browser.CloseAsync();290 }291 }292 }293 public async Task ReSendSmSCode(string Phone)294 {295 var page = GetPage(Phone);296 if (page == null) throw new Exception("页é¢æªæ¾å°,å¯è½è¶
æ¶åæ¶äº.请å·æ°é¡µé¢éæ°ç»å½");297 await GetPhoneCode(Phone, page);298 }299 /// <summary>300 /// å 为ç½ç»åºç°çå»é¼çå¾
代ç 301 /// </summary>302 /// <param name="page"></param>303 /// <returns></returns>304 public async Task<bool> AwitVerifyCode(Page page)305 {306 try307 {308 await page.WaitForTimeoutAsync(1000);309 // æå¼äº¬ä¸Appï¼è´ç©æ´è½»æ¾310 string js = "document.body.outerText";311 var pageouterText = await page.EvaluateExpressionAsync(js);312 var pagetext = pageouterText.ToString();313 314 if (pagetext.Contains("éªè¯ç è¾å
¥é误") || pagetext.Contains("éªè¯ç é误å¤æ¬¡ï¼è¯·éæ°è·å") || pagetext.Contains("该ææºå·æªæ³¨åï¼å°ä¸ºæ¨ç´æ¥æ³¨åã") || pagetext.Contains("æå¼äº¬ä¸Appï¼è´ç©æ´è½»æ¾"))315 {316 return true;317 }318 else319 {320 return await AwitVerifyCode(page);321 }322 }323 catch (Exception e)324 {325 return await AwitVerifyCode(page);326 }327 }328 public async Task<ResultModel<object>> VerifyCode(int qlkey,string qq, string Phone, string Code)329 {330 ResultModel<object> result = ResultModel<object>.Create(false, "");331 if (_mainConfig.UPTYPE == UpTypeEum.ql)332 {333 var qlconfig = _mainConfig.GetConfig(qlkey);334 if (qlconfig == null)335 {336 result.message = "æªæ¾å°ç¸åºçæå¡å¨é
ç½®ã请å·æ°é¡µé¢ååè¯";337 result.data = new { Status = 404 };338 return result;339 }340 }341 342 Page page = GetPage(Phone);343 if (page == null)344 {345 result.message = "æªæ¾å°å½åå·ç çç½é¡µè¯·ç¨ååè¯,æè
ç½é¡µè¶
è¿3åé已被åæ¶";346 result.data = new { Status = 404 };347 return result;348 }349 await SetCode(Code, page);350 //await page.WaitForTimeoutAsync(400);351 Console.WriteLine("è¾å
¥éªè¯ç " + Code);352 353 await page.ClickAsync("a[report-eventid='MLoginRegister_SMSLogin']");354 await AwitVerifyCode(page);355 string js = "document.body.outerText";356 var pageouterText = await page.EvaluateExpressionAsync(js);357 var pagetext = pageouterText.ToString();358 // Console.WriteLine(pagetext.ToString());359 if (pagetext.Contains("éªè¯ç è¾å
¥é误"))360 {361 result.message = "éªè¯ç è¾å
¥é误";362 return result;363 }364 if (pagetext.Contains("éªè¯ç é误å¤æ¬¡ï¼è¯·éæ°è·å"))365 {366 await PageClose(Phone);367 result.data = new { Status = 501 };368 result.message = "éªè¯ç é误å¤æ¬¡,请è¿ä¸åéä¹ååæ¥ã";369 return result;370 }371 if (pagetext.Contains("该ææºå·æªæ³¨åï¼å°ä¸ºæ¨ç´æ¥æ³¨åã"))372 {373 await PageClose(Phone);374 result.data = new { Status = 501 };375 result.message = "该ææºå·æªæ³¨å";376 return result;377 }378 if (pagetext.Contains("æå¼äº¬ä¸Appï¼è´ç©æ´è½»æ¾"))379 {380 var cookies = await page.GetCookiesAsync();381 var CKkey = cookies.FirstOrDefault(x => x.Name == "pt_key");382 var CKpin = cookies.FirstOrDefault(x => x.Name == "pt_pin");383 if (CKkey == null || CKpin == null)384 {385 await PageClose(Phone);386 result.message = "è·åCookie失败ï¼è¯·éè¯";387 result.data = new { Status = 404 };388 return result;389 }390 391 var CCookie = CKkey.Name + "=" + CKkey.Value + ";" + CKpin.Name + "=" + CKpin.Value + ";";392 await PageClose(Phone);393 Console.WriteLine(Phone + "è·åå°ck");394 if (_mainConfig.UPTYPE == UpTypeEum.ql)395 {396 result = await UploadQL(Phone, CCookie, CKpin.Value, qlkey);397 return result;398 }399 if (_mainConfig.UPTYPE == UpTypeEum.xdd)400 {401 result = await Uploadxdd(qq, CCookie);402 return result;403 }404 int tabcount = GetTableCount();405 result.data = new { tabcount = tabcount };406 result.success = true;407 result.data = new { tabcount = tabcount,ck=CCookie };408 }409 result.message = "ç»é失败,请å·æ°é¡µé¢";410 return result;411 }412 public async Task<ResultModel<object>> Uploadxdd(string qq, string ck)413 {414 //"code":200,"data":"null","message":"æ·»å æå"415 ResultModel<object> result = ResultModel<object>.Create(false, "");416 417 using (HttpClient client = new HttpClient())418 {419 Dictionary<string, string> dict = new Dictionary<string, string>420 {421 {"qq",qq},422 {"token", _mainConfig.XDDToken},423 {"ck", ck}424 };425 var resultd =await client.PostAsync(_mainConfig.XDDurl, new FormUrlEncodedContent(dict));426 string resultContent = resultd.Content.ReadAsStringAsync().Result;427 428 JObject j = JObject.Parse(resultContent);429 int tabcount = GetTableCount();430 if (j["code"].ToString() == "200")431 {432 result.success = true;433 result.message = "æ·»å xddæå!";434 }435 else436 {437 result.message = j["message"].ToString();438 }439 result.data = new { tabcount = tabcount};440 return result;441 }442 }443 public async Task<ResultModel<object>> UploadQL(string Phone,string ck,string ckpin , int qlkey)444 {445 ResultModel<object> result = ResultModel<object>.Create(false, "");446 var qlconfig = _mainConfig.GetConfig(qlkey);447 var Nickname = "";448 int MAXCount = qlconfig.QL_CAPACITY;449 Nickname = await GetNickname(ck);450 JArray data = await qlconfig.GetEnv();451 JToken env = null;452 var QLCount = 0;453 if (data != null)454 {455 env = data.FirstOrDefault(x => x["value"].ToString().Contains("pt_pin=" + ckpin + ";"));456 QLCount = data.Count;457 }458 string QLId = "";459 string timestamp = "";460 if (env == null)461 {462 if (QLCount >= MAXCount)463 {464 result.message = "ä½ æ¥æäºï¼æ²¡æå¤ä½çä½ç½®äº";465 result.data = new { Status = 501 };466 return result;467 }468 var addresult = await qlconfig.AddEnv(ck, Nickname);469 JObject addUser = (JObject)addresult.data[0];470 QLId = addUser["_id"].ToString();471 timestamp = addUser["timestamp"].ToString();472 }473 else474 {475 QLId = env["_id"].ToString();476 if (env["remarks"] != null)477 Nickname = env["remarks"].ToString();478 var upresult = await qlconfig.UpdateEnv(ck, QLId, Nickname);479 timestamp = upresult.data["timestamp"].ToString();480 }481 await qlconfig.Enable(QLId);482 var qin = await getCount(qlkey);483 await PageClose(Phone);484 result.success = true;485 result.data = new { qlid = QLId, nickname = Nickname, timestamp = timestamp, remarks = Nickname, qlkey = qlconfig.QLkey, ckcount = qin.ckcount, tabcount = qin.tabcount };486 return result;487 }488 public int GetTableCount()489 {490 string MaxTab = _mainConfig.MaxTab;491 var intabcount = GetPageCount();492 int tabcount = int.Parse(MaxTab) - intabcount;493 return tabcount;494 }495 public async Task<(int ckcount, int tabcount)> getCount(int qlkey)496 {497 var config = _mainConfig.GetConfig(qlkey);498 var qlcount = await config.GetEnvsCount();499 var ckcount = config.QL_CAPACITY - qlcount;500 string MaxTab = _mainConfig.MaxTab;501 var intabcount = GetPageCount();502 int tabcount = int.Parse(MaxTab) - intabcount;503 return (ckcount, tabcount);504 }505 private async Task Setphone(string phone, Page page)506 {507 await page.ClickAsync("input[report-eventid='MLoginRegister_SMSPhoneInput']", new PuppeteerSharp.Input.ClickOptions { ClickCount = 3 });508 await page.TypeAsync("input[report-eventid='MLoginRegister_SMSPhoneInput']", phone);509 await page.WaitForTimeoutAsync(200);510 }511 private async Task<bool> GetPhoneCode(string Phone, Page page)512 {513 page = AddPage(Phone, page);514 await Setphone(Phone, page);515 var CodeBtn = await page.XPathAsync("//button[@report-eventid='MLoginRegister_SMSReceiveCode']");516 var CodeProperties = await CodeBtn[0].GetPropertiesAsync();517 ///var url = await CodeBtn[0].EvaluateFunctionAsync<string>("e => e.setAttribute('class','active')");518 //await page.evaluate((el, value) => el.setAttribute('style', value),el.setAttribute('style', value519 // divHandle,520 // 'background: #0FF'521 // )522 // CodeBtn[0].EvaluateFunctionAsync523 //page.ev524 // CodeBtn.Append525 var CodeBtnClasses = CodeProperties["_prevClass"].ToString().Split(" ");526 Console.WriteLine(CodeProperties["_prevClass"].ToString());527 bool canSendCode = CodeBtnClasses.Contains("active");528 if (canSendCode)529 {530 return true;531 }532 else533 {534 await page.ReloadAsync();535 await page.WaitForTimeoutAsync(500);536 return await GetPhoneCode(Phone, page);537 }538 }539 private long GetTime()540 {541 TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);//ToUniversalTime()转æ¢ä¸ºæ åæ¶åºçæ¶é´,å»æçè¯ç´æ¥å°±ç¨å京æ¶é´542 return (long)ts.TotalSeconds;543 }544 private async Task<string> GetNickname(string cookie)545 {546 try547 {548 var url = @"https://me-api.jd.com/user_new/info/GetJDUserInfoUnion?orgFlag=JD_PinGou_New&callSource=mainorder&channel=4&isHomewhite=0&sceneval=2&_=" + GetTime() + "&sceneval=2&g_login_type=1&g_ty=ls";549 using (HttpClient client = new HttpClient())550 {551 client.DefaultRequestHeaders.Add("Cookie", cookie);552 client.DefaultRequestHeaders.Add("Referer", "https://home.m.jd.com/myJd/newhome.action");553 client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36");554 client.DefaultRequestHeaders.Add("Host", "me-api.jd.com");555 var result = await client.GetAsync(url);556 string resultContent = result.Content.ReadAsStringAsync().Result;557 Console.WriteLine("è·ånickname");558 JObject j = JObject.Parse(resultContent);559 // data?.userInfo.baseInfo.nickname560 return j["data"]["userInfo"]["baseInfo"]["nickname"].ToString();561 }562 }563 catch (Exception e)564 {565 Console.WriteLine("è·ånicknameåºé:"+e.Message);566 return "æªç¥";567 }568 }569 private static async Task SetCode(string Code, Page page)570 {571 await page.ClickAsync("#authcode", new PuppeteerSharp.Input.ClickOptions { ClickCount = 3 });572 await page.TypeAsync("#authcode", Code);573 }574 /// <summary>575 /// å 为ç½ç»åºç°çå»é¼çå¾
代ç 576 /// </summary>577 /// <param name="page"></param>578 /// <returns></returns>579 public async Task<bool> AwitAutoCaptcha(Page page)580 {581 try582 {583 await page.WaitForTimeoutAsync(1000);584 // æå¼äº¬ä¸Appï¼è´ç©æ´è½»æ¾585 string js = "document.body.outerText";...
VkBrwUserFactory.cs
Source:VkBrwUserFactory.cs
...61 ElementHandle noteworthyPages = noteworthyPagesBlock.First();62 ElementHandle[] noteworthyPagesHeaderBlock = await noteworthyPages.QuerySelectorAllAsync("a.module_header");63 if (noteworthyPagesHeaderBlock.Length == 1)64 {65 ClickOptions clickOptions = new ClickOptions { Delay = new Random().Next(30, 100) };66 ElementHandle noteworthyPagesLinkElement = noteworthyPagesHeaderBlock.First();67 await noteworthyPagesLinkElement.ClickAsync(clickOptions);68 ElementHandle noteworthyPagesIsOpened = await userPage.WaitForSelectorAsync("#fans_rowsidols.fans_rows.fans_idols");69 ElementHandle[] closeBlock = await userPage.QuerySelectorAllAsync("div.box_x_button");70 if (closeBlock.Length == 1)71 {72 ElementHandle[] pagesCountBlock = await userPage.QuerySelectorAllAsync("span.ui_box_header_cnt");73 if (pagesCountBlock.Length == 1)74 {75 ElementHandle pagesTotalCountElement = pagesCountBlock.First();76 string pagesTotalCountValue = await pagesTotalCountElement.EvaluateFunctionAsync<string>("('span', span => span.innerText)");77 if (!string.IsNullOrEmpty(pagesTotalCountValue))78 {79 bool pagesTotalCountReceived = int.TryParse(pagesTotalCountValue, out int pagesTotalCount);...
WebScraper.cs
Source:WebScraper.cs
...175 MouseClickAsync(button).Wait();176 }177 private async Task MouseClickAsync(MouseButton button)178 {179 await m_page.Mouse.ClickAsync(m_MouseX, m_MouseY, new ClickOptions{ Button = button == MouseButton.Left ? PuppeteerSharp.Input.MouseButton.Left : PuppeteerSharp.Input.MouseButton.Right });180 }181 /// <summary>182 /// Simulates a mouse up event on page.183 /// </summary>184 /// <param name="button">Mouse button to simulate.</param>185 public void MouseUp(MouseButton button)186 {187 MouseUpAsync(button).Wait();188 189 }190 private async Task MouseUpAsync(MouseButton button)191 {192 await m_page.Mouse.UpAsync(new ClickOptions { Button = button == MouseButton.Left ? PuppeteerSharp.Input.MouseButton.Left : PuppeteerSharp.Input.MouseButton.Right });193 }194 /// <summary>195 /// Simulates a mouse down event on page.196 /// </summary>197 /// <param name="button">Mouse button to simulate.</param>198 public void MouseDown(MouseButton button)199 {200 MouseDownAsync(button).Wait();201 }202 private async Task MouseDownAsync(MouseButton button)203 {204 await m_page.Mouse.DownAsync(new ClickOptions { Button = button == MouseButton.Left ? PuppeteerSharp.Input.MouseButton.Left : PuppeteerSharp.Input.MouseButton.Right });205 }206 /// <summary>207 /// Simulates a touch tap on a page.208 /// </summary>209 /// <param name="target">Javascript selector for element to tap on.</param>210 public void TapScreen(string target)211 {212 TapScreenAsync(target).Wait();213 }214 private async Task TapScreenAsync(string target)215 {216 await m_page.TapAsync(target);217 }218 /// <summary>...
Mouse.cs
Source:Mouse.cs
...50 }).ConfigureAwait(false);51 }52 }53 /// <summary>54 /// Shortcut for <see cref="MoveAsync(decimal, decimal, MoveOptions)"/>, <see cref="DownAsync(ClickOptions)"/> and <see cref="UpAsync(ClickOptions)"/>55 /// </summary>56 /// <param name="x"></param>57 /// <param name="y"></param>58 /// <param name="options"></param>59 /// <returns>Task</returns>60 public async Task ClickAsync(decimal x, decimal y, ClickOptions options = null)61 {62 options = options ?? new ClickOptions();63 await MoveAsync(x, y).ConfigureAwait(false);64 await DownAsync(options).ConfigureAwait(false);65 if (options.Delay > 0)66 {67 await Task.Delay(options.Delay).ConfigureAwait(false);68 }69 await UpAsync(options).ConfigureAwait(false);70 }71 /// <summary>72 /// Dispatches a <c>mousedown</c> event.73 /// </summary>74 /// <param name="options"></param>75 /// <returns>Task</returns>76 public Task DownAsync(ClickOptions options = null)77 {78 options = options ?? new ClickOptions();79 _button = options.Button;80 return _client.SendAsync("Input.dispatchMouseEvent", new Dictionary<string, object>()81 {82 { MessageKeys.Type, "mousePressed" },83 { MessageKeys.Button, _button },84 { MessageKeys.X, _x },85 { MessageKeys.Y, _y },86 { MessageKeys.Modifiers, _keyboard.Modifiers },87 { MessageKeys.ClickCount, options.ClickCount }88 });89 }90 /// <summary>91 /// Dispatches a <c>mouseup</c> event.92 /// </summary>93 /// <param name="options"></param>94 /// <returns>Task</returns>95 public Task UpAsync(ClickOptions options = null)96 {97 options = options ?? new ClickOptions();98 _button = MouseButton.None;99 return _client.SendAsync("Input.dispatchMouseEvent", new Dictionary<string, object>()100 {101 { MessageKeys.Type, "mouseReleased" },102 { MessageKeys.Button, options.Button },103 { MessageKeys.X, _x },104 { MessageKeys.Y, _y },105 { MessageKeys.Modifiers, _keyboard.Modifiers },106 { MessageKeys.ClickCount, options.ClickCount }107 });108 }109 }110}...
ClickFunction.cs
Source:ClickFunction.cs
...54 var button = this.GetParameterValue<MouseButtonType?>("button");55 var delay = this.GetParameterValue<int?>("delay");56 var clickCount = this.GetParameterValue<int?>("clickCount");57 58 ClickOptions clickOptions = null;59 if (button != null || delay != null || clickCount != null)60 {61 clickOptions = new ClickOptions();62 if (button != null)63 {64 clickOptions.Button = button.Value switch65 {66 MouseButtonType.Left => MouseButton.Left,67 MouseButtonType.Right => MouseButton.Right,68 MouseButtonType.Middle => MouseButton.Middle,69 MouseButtonType.None => MouseButton.None,70 _ => throw new ArgumentOutOfRangeException()71 };72 }73 if (delay != null)74 {75 clickOptions.Delay = delay.Value;76 }77 if (clickCount != null)78 {79 clickOptions.ClickCount = clickCount.Value;80 }81 }82 if (!string.IsNullOrEmpty(selector))83 {84 await this.ClickSelectorAsync(page, selector, clickOptions);85 }86 else if (x != null && y != null)87 {88 await this.ClickCoordinateAsync(page, x.Value, y.Value, clickOptions);89 }90 else91 {92 throw new ArgumentNullException(nameof(selector), "Selector or coordinate parameters required for click function!");93 }94 }95 private async Task ClickSelectorAsync(Page page, string selector, ClickOptions clickOptions)96 {97 var frame = this.GetParameterValue<string>("frame");98 if (string.IsNullOrEmpty(frame))99 {100 if (selector.StartsWith(XPathSelector.XPathSelectorToken))101 {102 var element = await page.QuerySelectorByXPath(selector);103 if (element != null)104 {105 await element.ClickAsync();106 }107 else108 {109 throw new Exception($"Node not found with '{selector}' selector on click function");110 }111 }112 else113 {114 await page.ClickAsync(selector, clickOptions); 115 } 116 }117 else118 {119 var currentFrame = page.Frames.FirstOrDefault(x => x.Name == frame);120 if (currentFrame != null)121 {122 if (selector.StartsWith(XPathSelector.XPathSelectorToken))123 {124 var element = await currentFrame.QuerySelectorByXPath(selector);125 if (element != null)126 {127 await element.ClickAsync();128 }129 else130 {131 throw new Exception($"Node not found with '{selector}' selector on click function");132 }133 }134 else135 {136 await currentFrame.ClickAsync(selector, clickOptions); 137 }138 }139 else140 {141 throw new Exception($"Frame not found with name '{frame}'");142 }143 }144 }145 146 private async Task ClickCoordinateAsync(Page page, int x, int y, ClickOptions clickOptions)147 {148 await page.Mouse.ClickAsync(x, y, clickOptions);149 }150 #endregion151 }152}...
IPuppeteerBrowser.cs
Source:IPuppeteerBrowser.cs
...16 Task<Page> WaitGetPage(Page page, string selector, string targetId);17 Task<Page> ClickAndGetPage(ElementHandle element, string targetId);18 Task<Page> ClickAndGetPage(ElementHandle element, string targetId, WaitForOptions waitForOptions);19 Task<string> GetContentAsync(Page page);20 Task ClickAsync(Page page, string clickPlace, ClickOptions clickOptions = null);21 Task<string> GetFramePage(string iframeSrc);22 Task<string> GetFramePage(Page page, string frameName);23 Task<string> GetVideoAdsOnUrl(Page page, string xPath);24 Task<string> GetScrolledPage(string videoUrl, int maxScroll, params WaitUntilNavigation[] waitUntilNavigations);25 Task<string> GetPagePermission(Page page, string permissionName);26 Task<string> ClickButtonForContinue(Page page, string buttonXPath, string adUrl,27 params WaitUntilNavigation[] waitUntilNavigations);28 /// <summary>29 /// Returns IpDetails by the specified url30 /// </summary>31 /// <param name="ipFetcherUrl">The ip details provider url.</param>32 /// <returns>The instance of <see cref="IpDetails"></see></returns>33 Task<IpDetails> GetIpDetails(string ipFetcherUrl);34 /// <summary>...
MouseButton.cs
Source:MouseButton.cs
2using Newtonsoft.Json.Converters;3namespace PuppeteerSharp.Input4{5 /// <summary>6 /// The type of button click to use with <see cref="Mouse.DownAsync(ClickOptions)"/>, <see cref="Mouse.UpAsync(ClickOptions)"/> and <see cref="Mouse.ClickAsync(decimal, decimal, ClickOptions)"/>7 /// </summary>8 [JsonConverter(typeof(StringEnumConverter), true)] 9 public enum MouseButton10 {11 /// <summary>12 /// Non specified13 /// </summary>14 None,15 /// <summary>16 /// The left mouse button17 /// </summary>18 Left,19 /// <summary>20 /// The right mouse button...
ClickOptions.cs
Source:ClickOptions.cs
2{3 /// <summary>4 /// Options to use when clicking5 /// </summary>6 public class ClickOptions7 {8 /// <summary>9 /// Time to wait between <c>mousedown</c> and <c>mouseup</c> in milliseconds. Defaults to 010 /// </summary>11 public int Delay { get; set; } = 0;12 /// <summary>13 /// Defaults to 1. See https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/detail14 /// </summary>15 public int ClickCount { get; set; } = 1;16 /// <summary>17 /// The button to use for the click. Defaults to <see cref="MouseButton.Left"/>18 /// </summary>19 public MouseButton Button { get; set; } = MouseButton.Left;20 }...
ClickOptions
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 var browser = await Puppeteer.LaunchAsync(new LaunchOptions10 {11 });12 var page = await browser.NewPageAsync();13 var searchBox = await page.QuerySelectorAsync("input[name='q']");14 await searchBox.TypeAsync("puppeteer");15 await searchBox.PressAsync("Enter");16 await page.WaitForNavigationAsync();17 var searchResult = await page.QuerySelectorAsync("h3");18 await searchResult.ClickAsync(new ClickOptions { ClickCount = 2 });19 await browser.CloseAsync();20 }21 }22}23using System;24using System.Threading.Tasks;25using PuppeteerSharp;26{27 {28 static async Task Main(string[] args)29 {30 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);31 var browser = await Puppeteer.LaunchAsync(new LaunchOptions32 {33 });34 var page = await browser.NewPageAsync();35 var searchBox = await page.QuerySelectorAsync("input[name='q']");36 await searchBox.TypeAsync("puppeteer");37 await searchBox.PressAsync("Enter");38 await page.WaitForNavigationAsync();39 var searchResult = await page.QuerySelectorAsync("h3");40 await searchResult.ClickAsync(new ClickOptions { ClickCount = 2 });41 await browser.CloseAsync();42 }43 }44}45using System;46using System.Threading.Tasks;47using PuppeteerSharp;48{49 {50 static async Task Main(string[] args)51 {52 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);53 var browser = await Puppeteer.LaunchAsync(new LaunchOptions54 {55 });
ClickOptions
Using AI Code Generation
1var options = new ClickOptions();2options.Button = MouseButton.Left;3options.ClickCount = 3;4await page.ClickAsync("button", options);5var options = new ClickOptions();6options.Button = MouseButton.Left;7options.ClickCount = 3;8await page.ClickAsync("button", options);9var options = new ClickOptions();10options.Button = MouseButton.Left;11options.ClickCount = 3;12await page.ClickAsync("button", options);13var options = new ClickOptions();14options.Button = MouseButton.Left;15options.ClickCount = 3;16await page.ClickAsync("button", options);17var options = new ClickOptions();18options.Button = MouseButton.Left;19options.ClickCount = 3;20await page.ClickAsync("button", options);21var options = new ClickOptions();22options.Button = MouseButton.Left;23options.ClickCount = 3;24await page.ClickAsync("button", options);25var options = new ClickOptions();26options.Button = MouseButton.Left;27options.ClickCount = 3;28await page.ClickAsync("button", options);29var options = new ClickOptions();30options.Button = MouseButton.Left;31options.ClickCount = 3;32await page.ClickAsync("button", options);33var options = new ClickOptions();34options.Button = MouseButton.Left;35options.ClickCount = 3;36await page.ClickAsync("button", options);37var options = new ClickOptions();38options.Button = MouseButton.Left;39options.ClickCount = 3;40await page.ClickAsync("button", options);
ClickOptions
Using AI Code Generation
1var options = new ClickOptions { Button = MouseButton.Left, ClickCount = 2 };2await page.ClickAsync("#btn", options);3var options = new ClickOptions { Button = MouseButton.Left, ClickCount = 2 };4await page.ClickAsync("#btn", options);5var options = new ClickOptions { Button = MouseButton.Left, ClickCount = 2 };6await page.ClickAsync("#btn", options);7var options = new ClickOptions { Button = MouseButton.Left, ClickCount = 2 };8await page.ClickAsync("#btn", options);9var options = new ClickOptions { Button = MouseButton.Left, ClickCount = 2 };10await page.ClickAsync("#btn", options);11var options = new ClickOptions { Button = MouseButton.Left, ClickCount = 2 };12await page.ClickAsync("#btn", options);13var options = new ClickOptions { Button = MouseButton.Left, ClickCount = 2 };14await page.ClickAsync("#btn", options);15var options = new ClickOptions { Button = MouseButton.Left, ClickCount = 2 };16await page.ClickAsync("#btn", options);17var options = new ClickOptions { Button = MouseButton.Left, ClickCount = 2 };18await page.ClickAsync("#btn", options);19var options = new ClickOptions { Button = MouseButton.Left, ClickCount = 2 };20await page.ClickAsync("#btn", options);21var options = new ClickOptions { Button = MouseButton.Left, ClickCount = 2 };22await page.ClickAsync("#btn", options);23var options = new ClickOptions { Button = MouseButton.Left,
ClickOptions
Using AI Code Generation
1{2};3await page.ClickAsync("#someId", clickOptions);4{5};6await page.ClickAsync("#someId", clickOptions);7{8};9await page.ClickAsync("#someId", clickOptions);10{11};12await page.ClickAsync("#someId", clickOptions);13{14};15await page.ClickAsync("#someId", clickOptions);16{17};18await page.ClickAsync("#someId", clickOptions);19{20};21await page.ClickAsync("#someId", clickOptions);22{23};24await page.ClickAsync("#someId", clickOptions);25{26};27await page.ClickAsync("#someId", clickOptions);28{29};30await page.ClickAsync("#someId", clickOptions);31{
ClickOptions
Using AI Code Generation
1var options = new ClickOptions {Delay = 1000};2await page.ClickAsync("#btn", options);3var options = new ClickOptions {Delay = 1000};4await page.ClickAsync("#btn", options);5var options = new ClickOptions {Delay = 1000};6await page.ClickAsync("#btn", options);7var options = new ClickOptions {Delay = 1000};8await page.ClickAsync("#btn", options);9var options = new ClickOptions {Delay = 1000};10await page.ClickAsync("#btn", options);11var options = new ClickOptions {Delay = 1000};12await page.ClickAsync("#btn", options);13var options = new ClickOptions {Delay = 1000};14await page.ClickAsync("#btn", options);15var options = new ClickOptions {Delay = 1000};16await page.ClickAsync("#btn", options);17var options = new ClickOptions {Delay = 1000};18await page.ClickAsync("#btn", options);19var options = new ClickOptions {Delay = 1000};20await page.ClickAsync("#btn", options);21var options = new ClickOptions {Delay = 1000};22await page.ClickAsync("#btn", options);23var options = new ClickOptions {Delay = 1000};24await page.ClickAsync("#btn", options);25var options = new ClickOptions {Delay = 1000};26await page.ClickAsync("#btn", options);
ClickOptions
Using AI Code Generation
1var clickOptions = new ClickOptions();2clickOptions.Button = MouseButton.Middle;3clickOptions.ClickCount = 1;4await page.ClickAsync("a", clickOptions);5var clickOptions = new ClickOptions();6clickOptions.Button = MouseButton.Right;7clickOptions.ClickCount = 1;8await page.ClickAsync("a", clickOptions);9var clickOptions = new ClickOptions();10clickOptions.Button = MouseButton.Left;11clickOptions.ClickCount = 1;12await page.ClickAsync("a", clickOptions);13var clickOptions = new ClickOptions();14clickOptions.Button = MouseButton.Middle;15clickOptions.ClickCount = 1;16await page.ClickAsync("a", clickOptions);17var clickOptions = new ClickOptions();18clickOptions.Button = MouseButton.Right;19clickOptions.ClickCount = 1;20await page.ClickAsync("a", clickOptions);21var clickOptions = new ClickOptions();22clickOptions.Button = MouseButton.Left;23clickOptions.ClickCount = 1;24await page.ClickAsync("a", clickOptions);25var clickOptions = new ClickOptions();26clickOptions.Button = MouseButton.Middle;27clickOptions.ClickCount = 1;28await page.ClickAsync("a", clickOptions);29var clickOptions = new ClickOptions();30clickOptions.Button = MouseButton.Right;31clickOptions.ClickCount = 1;32await page.ClickAsync("a", clickOptions);33var clickOptions = new ClickOptions();34clickOptions.Button = MouseButton.Left;35clickOptions.ClickCount = 1;36await page.ClickAsync("a", clickOptions);
ClickOptions
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4using PuppeteerSharp.Input;5using PuppeteerSharp.Input;6{7 {8 static void Main(string[] args)9 {10 MainAsync().Wait();11 }12 static async Task MainAsync()13 {14 var options = new LaunchOptions { Headless = false };15 using (var browser = await Puppeteer.LaunchAsync(options))16 using (var page = await browser.NewPageAsync())17 {18 await page.ClickAsync("input[name=q]");19 await page.Keyboard.TypeAsync("puppeteer-sharp");20 await page.ClickAsync("input[name=btnK]", new ClickOptions { Button = MouseButton.Right });21 await Task.Delay(10000);22 }23 }24 }25}26using System;27using System.Threading.Tasks;28using PuppeteerSharp;29using PuppeteerSharp.Input;30using PuppeteerSharp.Input;31{32 {33 static void Main(string[] args)34 {35 MainAsync().Wait();36 }37 static async Task MainAsync()38 {39 var options = new LaunchOptions { Headless = false };40 using (var browser = await Puppeteer.LaunchAsync(options))41 using (var page = await browser.NewPageAsync())42 {43 await page.ClickAsync("input[name=q]");44 await page.Keyboard.TypeAsync("puppeteer-sharp");45 await page.Keyboard.DownAsync("Shift");46 await page.ClickAsync("input[name=btnK]");47 await page.Keyboard.UpAsync("Shift");48 await Task.Delay(10000);49 }50 }51 }52}
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!!