WebView2ControlMBS focus on element and sendkeys

I was using HTMLViewer to log into a site that made sure the textbox was touched before the form could be submitted so I had to set focus and send keys. I put this into a timer and it worked great.

When putting the same code in WeView2ControlMBS I cannot get the focus or sendkeys to work at all. It doesn’t even focus on the textbox. web is name of my control. Is it not possible to set focus to an element and use sendkeys in WebView2ControlMBS? Thanks.

if refreshsecondstart = 1 then
web.SetFocus
web.ExecuteScript(“document.getElementById(‘username’).focus();”)
wsh.SendKeys("{BKSP}")
elseif refreshsecondstart = 2 then
wsh.SendKeys(user)
wsh.SendKeys("{TAB}")
elseif refreshsecondstart = 3 then
web.ExecuteScript(“document.getElementById(‘password’).focus();”)
wsh.SendKeys("{BKSP}")
elseif refreshsecondstart = 4 then
wsh.SendKeys(pass)
elseif refreshsecondstart = 5 then
wsh.SendKeys("{TAB}")
elseif refreshsecondstart = 6 then
wsh.SendKeys("{ENTER}")
end if

You may better need to synthesize JavaScript events.

function sendChangeEvent(ID) { var o = document.getElementById(ID); var evt = document.createEvent('Events'); evt.initEvent('change', true, true); o.dispatchEvent(evt); }

And call like sendChangeEvent(‘name’); where you pass ID for element. And instead of change also try input or blur. You may even send multiple of them.

Thanks Christian. This may be a little over my head, I’m just starting to learn JavaScript too. Thanks for the info, it will give me more JavaScript to read up on.

I did some more testing on this and it seems that sendkeys and setting the focus with JavaScript do work but I have to click on the page somewhere before JavaScript focus works. Once I click somewhere in the WebView2ControlMBS page it then will focus on elements. I’ve tried setting the focus to the control but that does not work, I need to click on the webpage somewhere to activate it it seems.