WebBrowser page operation extract get elements and tags (full article)

Source: Internet
Author: User

Recently using Webbrower to do a few hook applet, the collection accumulates as follows:

Using System;

Using System.Collections.Generic;

Using System.Linq;

Using System.Text;

Using system.windows.forms;//

Namespace WebBrowser Web operation

{

public class Element

{

Get elements by name

Public HtmlElement getelement_name (WebBrowser wb,string Name)

{

HtmlElement e = wb. Document.all[name];

return e;

}

Get elements by ID

Public HtmlElement getelement_id (WebBrowser wb, String Id)

{

HtmlElement e = wb. document.getElementById (ID);

return e;

}

Get elements based on index

Public HtmlElement getelement_index (WebBrowser wb,int Index)

{

HtmlElement e = wb. Document.all[index];

return e;

}

  //Get elements based on Type, use    public htmlelement getelement_type without name and ID (WebBrowser WB, String type)     {        htmlelement e=null;          htmlelementcollection elements = wb. document.getElementsByTagName ("input");         foreach (htmlelement element in elements)          {            if (element. GetAttribute ("type") = = Type)             {                 e = element;            }        }         return e;    }//Gets the element according to type, is used without name and ID, and specifies the first of the homogeneous type, Getelement_type () upgrade version  &NBSp; public htmlelement getelement_type_no (WebBrowser wb,string type,int i)     {         int j=1;         HtmlElement E=null;         htmlelementcollection elements = wb. document.getElementsByTagName ("input");         foreach (htmlelement element in elements)          {            if (element. GetAttribute ("type") = = Type)             {                 if (j==i)                   {                    e = element;                 }                  J + +;            }        }         return e;    }

Get form table sole name name, return form

Public HtmlElement getelement_form (WebBrowser wb,string form_name)

{

HtmlElement e = wb. Document.forms[form_name];

return e;

}

Set the value of the element Value property

public void Write_value (HtmlElement e,string value)

{

E.setattribute ("value", value);

}

Methods of executing elements, such as: Click,submit (requires form table sole name), etc.

public void Btn_click (HtmlElement e,string s)

{

E.invokemember (s);

}

}

}

This is the form code that calls this class:

Using System;

Using System.Collections.Generic;

Using System.ComponentModel;

Using System.Data;

Using System.Drawing;

Using System.Linq;

Using System.Text;

Using System.Windows.Forms;

Namespace WebBrowser Web operation

{

public partial class Form1:form

{

Element el = new element ();

Public Form1 ()

{

InitializeComponent ();

}

private void Form1_Load (object sender, EventArgs e)

{

WebBrowser1.Navigate (Application.startuppath + @ "\test.html");

}

private void Button1_Click (object sender, EventArgs e)

{

El. Write_value (el. Getelement_name (WebBrowser1, "username"), "isaced");

}

private void Button2_Click (object sender, EventArgs e)

{

El. Write_value (el. GETELEMENT_ID (webBrowser1, "password"), "123456");

}

private void Button3_Click (object sender, EventArgs e)

{

El. Btn_click (el. GETELEMENT_ID (WebBrowser1, "button"), "click");//method with the button click

}

private void Button4_Click (object sender, EventArgs e)

{

El. Btn_click (el. Getelement_form (WebBrowser1, "Form1"), "submit");//Get the form first, then call the form's Submit method

}

}

}

WebBrowser page operation extract get elements and tags (full article)

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.