OffScreen Example - Click Google search button using mouse down/up

This commit is contained in:
amaitland 2018-10-11 17:11:06 +10:00
parent 8ae0e132b3
commit b5ee0f5d0c

View File

@ -47,7 +47,7 @@ namespace CefSharp.MinimalExample.OffScreen
Cef.Shutdown(); Cef.Shutdown();
} }
private static void BrowserLoadingStateChanged(object sender, LoadingStateChangedEventArgs e) private static async void BrowserLoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{ {
// Check to see if loading is complete - this event is called twice, one when loading starts // Check to see if loading is complete - this event is called twice, one when loading starts
// second time when it's finished // second time when it's finished
@ -57,38 +57,49 @@ namespace CefSharp.MinimalExample.OffScreen
// Remove the load event handler, because we only want one snapshot of the initial page. // Remove the load event handler, because we only want one snapshot of the initial page.
browser.LoadingStateChanged -= BrowserLoadingStateChanged; browser.LoadingStateChanged -= BrowserLoadingStateChanged;
var scriptTask = browser.EvaluateScriptAsync("document.getElementById('lst-ib').value = 'CefSharp Was Here!'"); const string script = "(function(){ var elm = document.getElementById('lst-ib'); elm.value = 'CefSharp Was Here!'; return document.getElementsByName('btnK')[0].getBoundingClientRect(); })();";
scriptTask.ContinueWith(t => var jsResponse = await browser.EvaluateScriptAsync(script);
if(jsResponse.Success)
{ {
dynamic domRect = jsResponse.Result;
var x = (int)(domRect.x + (domRect.width / 2));
var y = (int)(domRect.y + (domRect.height / 2));
var host = browser.GetBrowserHost();
host.SendMouseClickEvent(x, y, MouseButtonType.Left, false, 1, CefEventFlags.None);
Thread.Sleep(15);
host.SendMouseClickEvent(x, y, MouseButtonType.Left, true, 1, CefEventFlags.None);
//Give the browser a little time to render //Give the browser a little time to render
Thread.Sleep(500); Thread.Sleep(2500);
// Wait for the screenshot to be taken. // Wait for the screenshot to be taken.
var task = browser.ScreenshotAsync(); var bitmap = await browser.ScreenshotAsync();
task.ContinueWith(x =>
{
// Make a file to save it to (e.g. C:\Users\jan\Desktop\CefSharp screenshot.png)
var screenshotPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CefSharp screenshot.png");
Console.WriteLine(); // Make a file to save it to (e.g. C:\Users\jan\Desktop\CefSharp screenshot.png)
Console.WriteLine("Screenshot ready. Saving to {0}", screenshotPath); var screenshotPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CefSharp screenshot.png");
// Save the Bitmap to the path. Console.WriteLine();
// The image type is auto-detected via the ".png" extension. Console.WriteLine("Screenshot ready. Saving to {0}", screenshotPath);
task.Result.Save(screenshotPath);
// We no longer need the Bitmap. // Save the Bitmap to the path.
// Dispose it to avoid keeping the memory alive. Especially important in 32-bit applications. // The image type is auto-detected via the ".png" extension.
task.Result.Dispose(); bitmap.Save(screenshotPath);
Console.WriteLine("Screenshot saved. Launching your default image viewer..."); // We no longer need the Bitmap.
// Dispose it to avoid keeping the memory alive. Especially important in 32-bit applications.
bitmap.Dispose();
// Tell Windows to launch the saved image. Console.WriteLine("Screenshot saved. Launching your default image viewer...");
Process.Start(screenshotPath);
Console.WriteLine("Image viewer launched. Press any key to exit."); // Tell Windows to launch the saved image.
}, TaskScheduler.Default); Process.Start(screenshotPath);
});
Console.WriteLine("Image viewer launched. Press any key to exit.");
}
} }
} }
} }