Operate on the JS pop-up dialog box in webbrowser and trigger other events ····

Source: Internet
Author: User

// Add an mshtml reference to the project
// Replace kernel functions

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using mshtml;

Namespace windowsapplication16
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}

Private void form1_load (Object sender, eventargs E)
{
Webbrowser1.navigate (@ "C: \ temp \ temp.htm ");
}

Private void webbrowserinclunavigated (Object sender, webbrowsernavigatedeventargs E)
{
Ihtmldocument2 vdocument = (ihtmldocument2) webbrowser1.document. domdocument;
Vdocument.parentwindow.exe cscript (
"Function alert (STR) {If (STR = 'zswang ') Confirm (STR );}","Javascript");
}
}
}

==========================================
How does one automatically call a button or <A> tag click event similar to the doc in the webbrowser control?

This. webbrowser1.document. Click + = new htmlelementeventhandler (document_click );

Void document_click (Object sender, htmlelementeventargs E)
{

If (readonly)
{
If (webbrowser1.document. activeelement. domelement is htmlanchorelement)
{
Htmlanchorelement em = (htmlanchorelement) webbrowser1.document. activeelement. domelement;
String href = em. href;
.......
}
}
}

Or:

Htmldocument hdoc = webbrowser. Document;
Htmlelement he object = hdoc. getelementbyid ("Object ID ");
He object. invokemember ("click ");

You can also use hdoc. getelementsbytagname ("tagname") to retrieve the object.

==========================================================
// C #2005 debugging successful

Private void webbrowserinclunavigated (Object sender, webbrowsernavigatedeventargs E)
{
Ihtmldocument2 vdocument = (ihtmldocument2) webbrowser1.document. domdocument;
Foreach (ihtmlelement velement in vdocument. All)
{
If (velement. tagname. toupper () = "frame ")
{
Ihtmlframebase2 vframebase2 = velement as ihtmlframebase2;
Vframebase2.contentwindow.exe cscript (
"Function alert (STR) {confirm ('[' + STR + ']');}", "JavaScript ");
}
}
}

========================================================== =
As a browser, how to obtain the specific link on the page and click it.

Actually relatively simple
Mshtml. ihtmldocument2 Doc = (mshtml. ihtmldocument2) This. axweb_browser.document;
Mshtml. ihtmlelementcollection myliks = Doc. links;
String tmp_ads_url = "xxxxxxx ";
Foreach (htmlanchorelementclass El in myliks)
{
If (El. href. tostring (). tolower () = tmp_ads_url)
{
El. Click ();
Break;
}
}

Okay. The problem that the current link does not open in a new window is also solved. The method is also very simple. You just need to find the specified link in my implementationCodeAdd
El.tar get = "_ Self ";
You don't have to say where to add it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.