Click here to Skip to main content
15,991,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
The following code in a WPF app with a webbrowser object that should be able to recognize the clicked html element of a website and give me the tag name. This works in normal sites like Google, etc. HOWEVER, these code is not working in some internal websites from our company.

For example, it works in this Google input object (text box) and shows tag name INPUT
<input class="gLFyf gsfi" maxlength="2048" name="q" type="text" jsaction="paste:puy29d" aria-autocomplete="both" aria-haspopup="false" autocapitalize="off" autocomplete="off" autocorrect="off" autofocus="" role="combobox" spellcheck="false" title="Search" value="" aria-label="Search" data-ved="0ahUKEwjfkO_z95LvAhXuMlkFHWsqBwEQ39UDCAY">


But, it doesn't work in this input object (text box) from our internal websites, and the click event is not even triggered.
<input id="Component24ff2cb_findCriteria" title="Search for records" hintlabelid="Component24ff2cb_findHintText" type="text" class="ms-crm-Dialog-Lookup-QuickFind" maxlength="100" tabindex="0" value="">


Any suggestion will be appreciated.

What I have tried:

public MainWindow()
{
    InitializeComponent();
    webBrowser1.LoadCompleted += new
    LoadCompletedEventHandler(webBrowser1_LoadCompleted);

}

private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
{
    mshtml.HTMLDocument doc;
    doc = (mshtml.HTMLDocument)webBrowser1.Document;
    mshtml.HTMLDocumentEvents2_Event iEvent;
    iEvent = (mshtml.HTMLDocumentEvents2_Event)doc;
    iEvent.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(ClickEventHandler);
}

private bool ClickEventHandler(mshtml.IHTMLEventObj e)
{
    textBox1.Text= e.srcElement.tagName
    return true;

}
Posted
Comments
Richard Deeming 4-Mar-21 12:44pm    
Is there an existing handler for the click event on the non-functioning site which cancels the event bubbling?

BTW, I assume you are aware that the WebBrowser control is stuck in IE7 mode by default? Most sites will not work properly when loaded in the control. You would probably do better to use the WebView2[^] or CefSharp[^] controls instead.
Omix_79 5-Mar-21 11:37am    
I am not sure if there is an existing handler, since it does work with a couple of elements, but for the rest of elements, it just doesn't.

About the webbrowser, I was told that this component should be used since we will be integrating this code with another.

The request is simple: We need to get the type or tagname of any clicked element of a website (loaded in the webbrowser).

I might consider another approach.

Thank you for your time to comment.
Richard Deeming 5-Mar-21 11:42am    
You could try using the mousedown event instead, and getting the element from the mouse position as described in this SO answer[^].
Omix_79 5-Mar-21 11:45am    
I will take a look and update later. Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900