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();
}
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
// 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.
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
Thread.Sleep(500);
Thread.Sleep(2500);
// Wait for the screenshot to be taken.
var task = 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");
var bitmap = await browser.ScreenshotAsync();
Console.WriteLine();
Console.WriteLine("Screenshot ready. Saving to {0}", screenshotPath);
// 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");
// Save the Bitmap to the path.
// The image type is auto-detected via the ".png" extension.
task.Result.Save(screenshotPath);
Console.WriteLine();
Console.WriteLine("Screenshot ready. Saving to {0}", screenshotPath);
// We no longer need the Bitmap.
// Dispose it to avoid keeping the memory alive. Especially important in 32-bit applications.
task.Result.Dispose();
// Save the Bitmap to the path.
// The image type is auto-detected via the ".png" extension.
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.
Process.Start(screenshotPath);
Console.WriteLine("Screenshot saved. Launching your default image viewer...");
Console.WriteLine("Image viewer launched. Press any key to exit.");
}, TaskScheduler.Default);
});
// Tell Windows to launch the saved image.
Process.Start(screenshotPath);
Console.WriteLine("Image viewer launched. Press any key to exit.");
}
}
}
}