How to use GetTitleAsync method of PuppeteerSharp.Page class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Page.GetTitleAsync

UCFullPageScreenShot.cs

Source:UCFullPageScreenShot.cs Github

copy

Full Screen

...161 OnShowTooltipInfo("初始化首页内容,开始切换国家");162 //var zips = Properties.Resources.zip.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);163 //var zip = zips.OrderBy(m => Guid.NewGuid()).First(); 164 await page.GoToAsync("https://www.amazon.com/");165 var pagetitle = await page.GetTitleAsync();166 try167 {168 var docid = _CountryModels.Find(m => m.Text.Contains(changeCountry));169 if (docid != null)170 {171 await page.EvaluateFunctionAsync(Properties.Resources.oChange2, docid.Id);172 }173 }174 catch (Exception ex)175 {176 MessageBox.Show("访问网址受限,自动切换地区失败,\r\n请等待亚马逊解封或切换新ip后再尝试\r\n" + ex.Message, pagetitle);177 return;178 }179 await Task.Delay(8000);180 }181 else if (randomCity)//没有选择国家的情况,选择地区切换地区182 {183 OnShowTooltipInfo("初始化首页内容,开始切换地区");184 var zips = Properties.Resources.zip.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);185 var zip = zips.OrderBy(m => Guid.NewGuid()).First();186 await page.GoToAsync("https://www.amazon.com/");187 var pagetitle = await page.GetTitleAsync();188 try189 {190 await InputMatchCaptch(page);191 await page.EvaluateFunctionAsync(Properties.Resources.oChange, zip);192 }193 catch (Exception ex)194 {195 MessageBox.Show("访问网址受限,自动切换地区失败,\r\n请等待亚马逊解封或切换新ip后再尝试", pagetitle);196 throw ex;197 }198 await Task.Delay(10000);199 }200 int index = 0;201 int max = urls.Length;...

Full Screen

Full Screen

PuppeteerMethods.cs

Source:PuppeteerMethods.cs Github

copy

Full Screen

...110 try111 {112 WaitUntilNavigation[] waitUntil = new[] { WaitUntilNavigation.Networkidle0, WaitUntilNavigation.Networkidle2, WaitUntilNavigation.DOMContentLoaded, WaitUntilNavigation.Load };113 await page.GoToAsync(url, new NavigationOptions { WaitUntil = waitUntil });114 var title = await page.GetTitleAsync();115 return new SuccessPuppeteerDataResult<string>(title, "Get Title Successfully");116 }117 catch (Exception ex)118 {119 return new ErrorPuppeteerDataResult<string>("Error Occured! Detail: " + ex.Message);120 }121 finally122 {123 CloseChromium(page);124 }125 }126 }127 public static async Task<Frame[]> GetAllFrames()128 {129 using (var page = await OpenChromiumPage())130 {131 WaitUntilNavigation[] waitUntil = new[] { WaitUntilNavigation.Networkidle0, WaitUntilNavigation.Networkidle2, WaitUntilNavigation.DOMContentLoaded, WaitUntilNavigation.Load };132 await page.GoToAsync("https://www.google.com", new NavigationOptions { WaitUntil = waitUntil });133 return page.Frames;134 }135 }136 public static async Task<PuppeteerResult> LoginFacebook(string username, string password)137 {138 using (var page = await OpenChromiumPage())139 {140 try141 {142 WaitUntilNavigation[] waitUntil = new[] { WaitUntilNavigation.Networkidle0, WaitUntilNavigation.Networkidle2, WaitUntilNavigation.DOMContentLoaded, WaitUntilNavigation.Load };143 await page.GoToAsync("https://tr-tr.facebook.com/", new NavigationOptions { WaitUntil = waitUntil });144 await page.WaitForSelectorAsync("input#email");145 //You must change your facebook login informations.146 await page.TypeAsync("input#email", username);147 await page.TypeAsync("input#pass", password);148 await page.ClickAsync("button[name='login']");149 await page.WaitForNavigationAsync();150 var HtmlContent = await page.GetContentAsync();151 var Cookie = await page.GetCookiesAsync();152 var TitleOfPage = await page.GetTitleAsync();153 var filePath = "D:\\PdfFiles";154 var fileName = Guid.NewGuid() + ".pdf";155 if (!Directory.Exists(filePath))156 Directory.CreateDirectory(filePath);157 await page.PdfAsync(Path.Combine(filePath, fileName));158 return new SuccessPuppeteerResult("Pdf Created Succesfully");159 }160 catch (Exception ex)161 {162 return new ErrorPuppeteerResult("Error Occured! Detail: " + ex.Message);163 }164 finally165 {166 CloseChromium(page);...

Full Screen

Full Screen

SaveWebPage.xaml.cs

Source:SaveWebPage.xaml.cs Github

copy

Full Screen

...51 screenshotOptions.OmitBackground = false;//是否使用透明背景,而不是默认白色背景52 screenshotOptions.Quality = 100; //截图质量 0-100(png不可用)53 screenshotOptions.Type = PuppeteerSharp.ScreenshotType.Jpeg; //截图格式5455 var fileName = Environment.CurrentDirectory + $"\\download\\{await page.GetTitleAsync()}.jpg";5657 if (System.IO.File.Exists(fileName))58 {59 fileName = fileName.Replace(".jpg", $"{DateTime.Now.ToString("ffff")}.jpg");60 }6162 await page.ScreenshotAsync(fileName, screenshotOptions);6364 if (System.IO.File.Exists(fileName))65 {66 BitmapImage bi = new BitmapImage();67 bi.BeginInit();68 bi.UriSource = new Uri(fileName, UriKind.Absolute);69 bi.EndInit();70 this.image.Source = bi;71 }72 else73 {74 EMessageBox.Show("保存网页截图失败");75 }7677 //在最后记得关闭浏览器及释放资源78 browser.Disconnect();79 browser.Dispose();80 }81 catch(Exception ex)82 {83 EMessageBox.Show(ex.Message);84 }85 }8687 private async void btn_SaveAsPDF_Click(object sender, RoutedEventArgs e)88 {89 //打开网页的操作跟上面是一样的90 try91 {92 await new PuppeteerSharp.BrowserFetcher().DownloadAsync(PuppeteerSharp.BrowserFetcher.DefaultRevision);93 var browser = await PuppeteerSharp.Puppeteer.LaunchAsync(new PuppeteerSharp.LaunchOptions94 {95 Headless = true96 });97 var page = await browser.NewPageAsync(); //打开一个新标签98 await page.GoToAsync(this.tbox_Url.Text); //访问页面99100 //设置PDF选项101 PuppeteerSharp.PdfOptions pdfOptions = new PuppeteerSharp.PdfOptions();102 pdfOptions.DisplayHeaderFooter = false; //是否显示页眉页脚103 pdfOptions.FooterTemplate = ""; //页脚文本104105 var width = await page.EvaluateFunctionAsync<int>("function getWidth(){return document.body.scrollWidth}");106 var height = await page.EvaluateFunctionAsync<int>("function getHeight(){return document.body.scrollHeight}");107108 pdfOptions.Width = $"{width}px";109 pdfOptions.Height = $"{height}px";110111 pdfOptions.HeaderTemplate = ""; //页眉文本112 pdfOptions.Landscape = false; //纸张方向 false-垂直 true-水平113 pdfOptions.MarginOptions = new PuppeteerSharp.Media.MarginOptions() { Bottom = "0px", Left = "0px", Right = "0px", Top = "0px" }; //纸张边距,需要设置带单位的值,默认值是None114 pdfOptions.Scale = 1m; //PDF缩放,从0-1115 pdfOptions.PrintBackground = true;116117 var fileName = Environment.CurrentDirectory + $"\\download\\{await page.GetTitleAsync()}.pdf";118119 if (System.IO.File.Exists(fileName))120 {121 fileName = fileName.Replace(".pdf", $"{DateTime.Now.ToString("ffff")}.pdf");122 }123124 //保存PDF125 await page.PdfAsync(fileName, pdfOptions);126 EMessageBox.Show($"{fileName}保存成功");127128 //在最后记得关闭浏览器及释放资源129 browser.Disconnect();130 browser.Dispose();131 } ...

Full Screen

Full Screen

PageObject.cs

Source:PageObject.cs Github

copy

Full Screen

...39 private async Task VerifyPageTitle(PageObject result)40 {41 if (!string.IsNullOrWhiteSpace(result.PageTitle))42 {43 var actualTitle = await Page.GetTitleAsync();44 if (Constants.TreatPageTitleMismatchAsError)45 {46 Assert.AreEqual(result.PageTitle, actualTitle, $"Page title did not match - {result.GetType().Name}");47 }48 else49 {50 if (result.PageTitle != actualTitle)51 {52 Console.WriteLine($"Warning: Page title did not match - {result.GetType().Name}");53 }54 }55 }56 }57 }...

Full Screen

Full Screen

StackOverflowService.cs

Source:StackOverflowService.cs Github

copy

Full Screen

...35 var section = response.Substring(startIndex - 12, closeIndex - startIndex + 12);36 return new StackoverflowQuestionDTO()37 {38 QuestionHtml = section,39 Title = (await _page.GetTitleAsync())[..^17],40 QuestionUrl = _page.Url41 };42 }43 private async Task<string> GetStackOverFlowQuestion(string questionId)44 {45 await _page.GoToAsync($"https://stackoverflow.com/questions/{questionId}");46 return await _page.GetContentAsync();47 }48 }49}...

Full Screen

Full Screen

Title.cs

Source:Title.cs Github

copy

Full Screen

...24 Headless = true,25 });26 var page = await browser.NewPageAsync();27 await page.GoToAsync(url);28 var title = await page.GetTitleAsync();29 await browser.CloseAsync();30 return new OkObjectResult(title);31 }32 }33}...

Full Screen

Full Screen

PdfHelper.cs

Source:PdfHelper.cs Github

copy

Full Screen

...5 public class PdfHelper6 {7 public static async Task<string> CreateFileNameAsync(Page page)8 {9 var pageTitle = await page.GetTitleAsync();10 if (string.IsNullOrWhiteSpace(pageTitle))11 return $"pdf-document-{DateTime.Now:yyyyMMddTHHmmss}.pdf";12 foreach (var c in Path.GetInvalidFileNameChars())13 pageTitle = pageTitle.Replace(c, '_');14 return $"{pageTitle}.pdf";15 }16 public static PaperFormat ParsePaperFormat(string format)17 {18 return format switch19 {20 "A0" => PaperFormat.A0,21 "A1" => PaperFormat.A1,22 "A2" => PaperFormat.A2,23 "A3" => PaperFormat.A3,...

Full Screen

Full Screen

TitleTests.cs

Source:TitleTests.cs Github

copy

Full Screen

...12 [Fact]13 public async Task ShouldReturnThePageTitle()14 {15 await Page.GoToAsync(TestConstants.ServerUrl + "/input/button.html");16 Assert.Equal("Button test", await Page.GetTitleAsync());17 }18 }19}...

Full Screen

Full Screen

GetTitleAsync

Using AI Code Generation

copy

Full Screen

1using System.Threading.Tasks;2using PuppeteerSharp;3{4 static async Task Main(string[] args)5 {6 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);7 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false }))8 using (var page = await browser.NewPageAsync())9 {10 string title = await page.GetTitleAsync();11 System.Console.WriteLine(title);12 }13 }14}15using System.Threading.Tasks;16using PuppeteerSharp;17{18 static async Task Main(string[] args)19 {20 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);21 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false }))22 using (var page = await browser.NewPageAsync())23 {24 string title = await page.GetTitleAsync();25 System.Console.WriteLine(title);26 }27 }28}29using System.Threading.Tasks;30using PuppeteerSharp;31{32 static async Task Main(string[] args)33 {34 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);35 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false }))36 using (var page = await browser.NewPageAsync())37 {38 string title = await page.GetTitleAsync();39 System.Console.WriteLine(title);40 }41 }42}43using System.Threading.Tasks;44using PuppeteerSharp;45{46 static async Task Main(string[] args)47 {48 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);49 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false }))50 using (var page = await browser.NewPageAsync())51 {

Full Screen

Full Screen

GetTitleAsync

Using AI Code Generation

copy

Full Screen

1var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });2var page = await browser.NewPageAsync();3var title = await page.GetTitleAsync();4Console.WriteLine(title);5await browser.CloseAsync();6var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });7var page = await browser.NewPageAsync();8var title = await page.GetTitleAsync();9Console.WriteLine(title);10await browser.CloseAsync();11var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });12var page = await browser.NewPageAsync();13var title = await page.GetTitleAsync();14Console.WriteLine(title);15await browser.CloseAsync();16var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });17var page = await browser.NewPageAsync();18var title = await page.GetTitleAsync();19Console.WriteLine(title);20await browser.CloseAsync();21var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });22var page = await browser.NewPageAsync();23var title = await page.GetTitleAsync();24Console.WriteLine(title);25await browser.CloseAsync();26var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });27var page = await browser.NewPageAsync();28var title = await page.GetTitleAsync();29Console.WriteLine(title);30await browser.CloseAsync();31var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });

Full Screen

Full Screen

GetTitleAsync

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 {9 };10 using (var browser = await Puppeteer.LaunchAsync(options))11 {12 using (var page = await browser.NewPageAsync())13 {14 Console.WriteLine("Page title is: " + await page.GetTitleAsync());15 }16 }17 }18 }19}

Full Screen

Full Screen

GetTitleAsync

Using AI Code Generation

copy

Full Screen

1using System.Threading.Tasks;2using PuppeteerSharp;3{4 {5 static async Task Main(string[] args)6 {7 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);8 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))9 using (var page = await browser.NewPageAsync())10 {11 string title = await page.GetTitleAsync();12 System.Console.WriteLine(title);13 }14 }15 }16}

Full Screen

Full Screen

GetTitleAsync

Using AI Code Generation

copy

Full Screen

1var page = await browser.NewPageAsync();2var title = await page.GetTitleAsync();3Console.WriteLine(title);4await browser.CloseAsync();5var page = await browser.NewPageAsync();6var title = await page.GetTitleAsync();7Console.WriteLine(title);8await browser.CloseAsync();9var page = await browser.NewPageAsync();10var title = await page.GetTitleAsync();11Console.WriteLine(title);12await browser.CloseAsync();13var page = await browser.NewPageAsync();14var title = await page.GetTitleAsync();15Console.WriteLine(title);16await browser.CloseAsync();17var page = await browser.NewPageAsync();18var title = await page.GetTitleAsync();19Console.WriteLine(title);20await browser.CloseAsync();21var page = await browser.NewPageAsync();22var title = await page.GetTitleAsync();23Console.WriteLine(title);24await browser.CloseAsync();25var page = await browser.NewPageAsync();26var title = await page.GetTitleAsync();27Console.WriteLine(title);28await browser.CloseAsync();29var page = await browser.NewPageAsync();30await page.GoToAsync("

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.

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in Page

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful