Tab to recaptcha

别来无恙 提交于 2019-12-02 16:26:43

问题


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 recaptcha.

I've tried using SendKeys to tab to the captcha, but instead of the tab being pressed once it seems to register multiple times and ends up focusing on the 'Terms' link in the bottom right of the captcha instead of the checkbox.

I've also tried using the chromeBrowser.ExecuteScriptAsync() method to run javascript on the page. I can successfully focus on textboxes and even run keypress events from those textboxes, but when I run a tab keypress event on the preceding textbox the recaptcha is still not highlighted.

chromeBrowser.Focus();
chromeBrowser.ExecuteScriptAsync("document.getElementsByClassName('txtBox')[0].Focus();" +
"document.getElementsByClassName('txtBox')[0].value = 'test string';")
SendKeys.Send("{TAB}");
SendKeys.Send("{ENTER}");

I've also added an event listener that confirms that the Tab is indeed being 'pressed'

chromeBrowser.Focus();
chromeBrowser.ExecuteScriptAsync("document.getElementsByClassName('txtBox')[0].addEventListener('keypress', function(){" +
"alert('something was pressed!');});" +
"document.getElementsByClassName('txtBox')[0].focus();" +
"document.getElementsByClassName('txtBox')[0].value = 'test value';" +
"var e = document.createEvent('KeyboardEvent');" +
"e.initKeyboardEvent('keypress', true, true, document.defaultView, 9, 0, '', false, '');" +
"document.getElementsByClassName('txtBox')[0].dispatchEvent(e);");

I've tried getElementsByClassName for every classname that shows on the recapctha in a page inspect and then setting focus to that. I've tried getElementById on the ID of the recaptcha iFrame and setting focus to that. Nothing focuses on the captcha.

I'm out of ideas. Any help would be appreciated.


回答1:


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);


来源:https://stackoverflow.com/questions/42947599/tab-to-recaptcha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!