Webbrowser control onclick event

孤街醉人 提交于 2021-01-27 23:28:06

问题


I have a code that clicks an html element but it doesn't fire its java script code, in VBA I used to call "initEvent" in order to invoke a java script event. I have searched the web and haven't found a suitable solution.

There's a working solution, but it's not quite useful in most cases, placing a java script code in the url and navigating to it, however there's a need for the location of the html element which is a problem sometimes.

The code I use in order to do a click in the html element :

el.InvokeMember("onclick");

Also tried :

el.InvokeMember("click");

The code that I place in the url and find not too much useful :

wb.Navigate("javascript: document.getElementsByClassName('something')[0].click();void(0);");

I hope to receive a working solution, thanks in advance.


回答1:


I've solved it, a very original solve I'd say. So basically, I've thought about it this way. Finding an element that I already have is not comfortable at all, so the java script navigate didn't seem useful, however, I thought of changing it to fit any element, the question was how.

The answer I came up with, what if I'd focus the specific element and then use a javascript to click on the focused element, and it worked.

So the fixed code :

el.Focus();
wb.Navigate("javascript: document.activeElement.click();void(0);");

I hope it'll be useful to some of you.



来源:https://stackoverflow.com/questions/51550636/webbrowser-control-onclick-event

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