ScreenshotDataAsync method never returns if a Page object is being created on a separate thread.
See original GitHub issueDescription
ScreenshotDataAsync
method never returns if the code creates a new browser page during that operation.
The purpose for this is to create an auto-refillable ConcurrentQueue of pages that would provide the next available page to a client that asks for it. The idea is to create 20 pages via NewPageAsync
and hand them out to clients who ask for a page. When the amount of available pages gets down to 3, create more pages.
Complete minimal example reproducing the issue
The code starts the browser, creates a page, goes to a new thread to create the screenshot. Meanwhile, the main thread creates 20 more new pages.
void Main(string[] args) {
var options = new LaunchOptions() {
// replace with your own or downllad
ExecutablePath = @"C:\Projects\net2\Tools\WidgetImages\chromium\chrome.exe",
Headless = false
};
Browser browser = Puppeteer.LaunchAsync(options).Result;
Page page = browser.NewPageAsync().Result;
Task.Run(() => TakeScreenshot(page, "https://www.google.com"));
// create a bunch of pages
var pagesQueue = new ConcurrentQueue<Page>();
for (int i = 0; i < 20; i++) {
Debug.WriteLine(DateTime.Now.Ticks + $" CreatePage {i}");
pagesQueue.Enqueue(browser.NewPageAsync().Result);
}
Console.ReadKey();
}
public static void TakeScreenshot(Page page, string url) {
var viewportOptions = new ViewPortOptions {
Width = 800,
Height = 800
};
page.SetViewportAsync(viewportOptions).Wait();
var webPage = page.GoToAsync(url).Result;
// get the screenshot
Debug.WriteLine("ScreenshotDataAsync start");
var imageBytes = page.ScreenshotDataAsync().Result;
Debug.WriteLine("ScreenshotDataAsync End");
var ms = new MemoryStream(imageBytes);
Image img = Image.FromStream(ms);
string desktopDir = Environment.GetFolderPath(SpecialFolder.DesktopDirectory);
string path = Path.Combine(desktopDir, "img1.png");
img.Save(path);
}
Expected behavior:
The ScreenshotDataAsync
method generates a screenshot.
Actual behavior:
The ScreenshotDataAsync
method never returns.
Versions
- PuppeteerSharp 1.14
- .NET Core 2.2.
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (7 by maintainers)
Top Results From Across the Web
ScreenshotDataAsync method never returns if a Page ...
The code starts the browser, creates a page, goes to a new thread to create the screenshot. Meanwhile, the main thread creates 20...
Read more >Use PuppeteerSharp.ElementHandle. ...
Use the ScreenshotDataAsync method in PuppeteerSharp package in your next Puppeteer-sharp project with LambdaTest Automation Testing Advisor.
Read more >Convert HTML string to image
Now I want to create an image from this string content which actually holds the HTML markup. I don't want to create the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Just found out it was caused by something else. Screenshot is working fine. sorry for the trouble.
Hi, How did you solve it or what was the problem? I have the same problem, could you help me with that?