Simple port of Ghost Cursor to .NET.
Ghost Cursor is a library for CefSharp and PuppeteerSharp that moves the mouse cursor in a more natural way.
Install one of the following NuGet packages:
- For PuppeteerSharp:
GhostCursor.PuppeteerSharp
- For CefSharp WinForms in .NET Framework:
GhostCursor.CefSharp.WinForms
- For CefSharp WinForms in .NET (Core):
GhostCursor.CefSharp.WinForms.NetCore
- For Selenium:
GhostCursor.Selenium
When trying to click an element, the library checks if:
- The element is in the DOM, if not, throws
CursorElementNotFoundException
- The element is in the viewport, if not, it scrolls to the element.
- The element is visible (e.g. the display is not set to none), if not, throws
CursorElementNotVisibleException
- The element is clickable (e.g. there is no popup blocking the element), if not, throws
CursorElementNotClickableException
.
using System.Threading.Tasks;
using GhostCursor;
using GhostCursor.PuppeteerSharp;
using PuppeteerSharp;
// Initialize PuppeteerSharp
using var browserFetcher = new BrowserFetcher();
await browserFetcher.DownloadAsync();
await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = false
});
await using var page = await browser.NewPageAsync();
await page.GoToAsync("https://www.google.com/?hl=en");
// Create the cursor
var options = new CursorOptions
{
Debug = true,
DefaultSteps = 20
};
var cursor = page.CreateCursor(options);
// Search for "Hello world" with the cursor
await using (await cursor.StartAsync())
{
await cursor.ClickAsync(ElementSelector.FromXPath("//div[text() = 'Reject all']"));
await cursor.ClickAsync("[title='Search']");
await cursor.TypeAsync("Hello world");
await cursor.ClickAsync("input[value='Google Search']");
await page.WaitForNavigationAsync();
}
// Wait for 2 seconds before closing the browser
await Task.Delay(2000);