Here\'s the scenario:
I\'m loading a page via a CefSharp browser. When the user clicks a certain button (on the form, not the web page) the page should then focus on the
I found a solution that avoided SendKeys and javascript entirely. The CefSharp browser has the ability to simulate key presses and mouse clicks. I ended up just selecting the textbox that preceded the captcha and then tabbing to it.
chromeBrowser.Focus();
chromeBrowser.ExecuteScriptAsync("document.getElementsByClassName('txtBox')[0].focus();");
CefSharp.KeyEvent keyEvent = new CefSharp.KeyEvent();
keyEvent.WindowsKeyCode = (int)Keys.Tab;
chromeBrowser.GetBrowser().GetHost().SendKeyEvent(keyEvent);
I've tested KeyDown
, KeyUp
for Tab key and it works but to put a char in a input element we need to use KeyEventType.Char
instead. even Tab key in this case act as text tab not for changing focus.
KeyEvent key = new KeyEvent
{
WindowsKeyCode = 0x0D, // Enter you could use '(int)Keys.Enter'
FocusOnEditableField = true,
IsSystemKey = false,
Type = KeyEventType.Char
};
_browser.GetBrowser().GetHost().SendKeyEvent(key);