WebBrowser control use (belongs to the personal diary, I am also a novice)

Source: Internet
Author: User
Tags tagname

Web simulation I know 2 kinds


1: Get the page via HttpRequest's Get/post submission method

2: Use C#webbrowser control to implement page emulation click

When the page has an encryption algorithm, or a random parameter is not within the scope of the solution to use the 2nd, simple Web page recommended the use of the 1th!

WebBrowser use


In the Lode event

private void Form1_Load (Object Sender,eventargs e)
{

WebBrowser1.Navigate ("page url");

}

Implement WEBBROWSER1 Display page, trigger documentcompleted when loading is complete

private void Webbrowser1_documentcompleted (Objectsender, WebBrowserDocumentCompletedEventArgs e)
{

WebBrowser1.Document.GetElementById ("tag id"). InnerText = "I need to fill in the characters";//Completion Login Auto Fill

              htmldocument doca = this.webBrowser1.Document;// Instantiate the current WebBrowser1 displayed document as a HTMLDocument object
               for (int i = 0; I <d Oca. All.count; i++)//loop to find each element of this object
              {

if (Doca. All[i]. TagName = = "button")//If this element is a
{
HtmlElement myelement =doca. All[i]; This element is instantiated into a HtmlElement object
if (myelement. outertext== "Login" && myelement. Id = = "J_submitstatic")//if the text of this element is "next page"
{
MyElement. InvokeMember ("click"); Click on this element
Break
}
}

The above DocumentCompleted event by traversing the button tag in the source of the Web page to find the ID equals "j_submitstatic" display character "Login" buttons to click

This time the page will automatically simulate the click Effect, to achieve the page jump.

A lot of people here directly through the webbrowser1.document to get the source of the page, but found that the page source code has been the source of the page before the login

I've been stuck here for a long time, and a lot of smart people know that there will be separate events to get the code after the jump. Test to see the WebBrowser1 navigated event that is triggered when navigating to a new document use Webbrowser1.document successfully obtained the source after the jump.

private void Webbrowser1_navigated (Objectsender, WebBrowserNavigatedEventArgs e)
{

String Yuanma =webbrowser1.document;//Saves the page source to a string string

}

A lot of people come here basic needs can be completed, after all, the page is basically the jump based on the replacement of the URL to achieve the page jump.

But the problem again, many well-known web pages are hidden in a lot of hyperlinks in JS, want to simply walk through the source a tag to find and click on the page a tag can not be found.

At this time we need to know the role of cookies in the browser, the people who have developed the Web site know that page login after the page's control of the delivery permissions are based on a cookie. When we log in, the equivalent of getting an identity card on the page of the unobstructed browsing is based on this authentication.

Do you know that the WebBrowser control will record our cookies?

Let's start testing:

int count = 0;

private void Form1_Load (Object Sender,eventargs e)
{

if (count== 0)
{
WebBrowser1.Navigate ("login page");
}
else if (count = = 1)
{
WebBrowser1.Navigate ("Any hyperlink that can be viewed after login");

Count = 2;
}

}

private void Webbrowser1_documentcompleted (Objectsender, WebBrowserDocumentCompletedEventArgs e)
{

if (webbrowser1.readystate = = webbrowserreadystate.complete)//Determine if loading is complete
{

WebBrowser1.Document.GetElementById ("tag id"). InnerText = "I need to fill in the characters";//Completion Login Auto Fill

              htmldocument doca = this.webBrowser1.Document;// Instantiate the current WebBrowser1 displayed document as a HTMLDocument object
               for (int i = 0; I <d Oca. All.count; i++)//loop to find each element of this object
              {

if (Doca. All[i]. TagName = = "button")//If this element is a
{
HtmlElement myelement =doca. All[i]; This element is instantiated into a HtmlElement object
if (myelement. outertext== "Login" && myelement. Id = = "J_submitstatic")//if the text of this element is "next page"
{
MyElement. InvokeMember ("click"); Click on this element
Break
}

}
}

private void Webbrowser1_navigated (Objectsender, WebBrowserNavigatedEventArgs e)
{

Stringyuanma = webbrowser1.document; Save page Source to string string

if (Yuanma. Contains ("A unique field in a successfully displayed page")//determine if the string is in the source of the successful login page to confirm the login success

{

Count = 1;//Login successfully changed global variable to 1

Set a timer interval trigger, about the use of the timer will not Baidu it!

System.Timers.Timer timer = new System.Timers.Timer (3000);//Instantiate timer class, set interval time is 1000 milliseconds is 1 seconds;
Timer. Enabled = true;
Timer. Start ();
Timer. Elapsed + = new System.Timers.ElapsedEventHandler (form1_load);//Trigger Load Event
}
}

private void Timer1_elapsed (Object Sender,system.timers.elapsedeventargs e)
{

Application.restart ();

}

Because the global variable count value changes when the Load event is triggered

if (count== 0)
{
WebBrowser1.Navigate ("login page");
}
else if (count = = 1)
{
WebBrowser1.Navigate ("Any hyperlink that can be viewed after login");

Count = 2;
}

The WebBrowser control loads the WebBrowser1.Navigate ("any hyperlink that can be viewed after login"); The link. If a cookie exists, it is directly redirected because it has permission.

The test found that the WebBrowser control has the ability to remember cookies, so we can get the source code of the page at this time. (Many people say to get the source code for what, because I do this is the need to get the Web information real-time storage search)

Many people think this is basically no problem now, the ability to get the source code has been completed. As long as each time in the navigated event to obtain the source code to add a judgment can be accurate to get their own source code. I feel the same, but there is always an exception to some of the most famous pages of his page with a static refresh effect URL is not changed to implement the jump, the specific situation I am not very clear but just before suddenly I want to get source code discovery can not get, through the breakpoint debugging found the source is incomplete, unable to find their own information.

Navigated triggered when navigating to a new document so I personally feel that it is possible that the trigger condition is not reached or the time is not ready to lead to the early operation so that the source is incomplete, but how to change the effect you want to change. In the end, I suddenly think that the source code we get in the DocumentCompleted event is always the link contained in WebBrowser1.Navigate (""), and I change the WebBrowser1.Navigate ("") by triggering the Load event URL to see if the source code for this URL will be available after loading.

Through the change of count time trigger plus judgment found can be achieved, the basic requirements have been met!

private void Webbrowser1_documentcompleted (object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webbrowser1.readystate = = Webbrowserreadystate.complete)
{

                        Encoding Encoding = Encoding.getenc Oding (webBrowser1.Document.Encoding);
                        StreamReader stream = new Streamre Ader (Webbrowser1.documentstream, encoding);
                        string SS = Stream. ReadToEnd ();

                        if (ss. Contains ("J_submit"))
                        {
  & nbsp                              webbrowser1.script Errorssuppressed = true;

WebBrowser1.Document.GetElementById ("Tpl_username_1"). InnerText = "Account";

WebBrowser1.Document.GetElementById ("Tpl_password_1"). Focus ();

WebBrowser1.Document.GetElementById ("Tpl_password_1"). InnerText = "Password"

HTMLDocument doca = this.webBrowser1.Document; Instantiate the current WebBrowser1 displayed document as a HTMLDocument object
for (int i = 0; i < Doca. All.count; i++)//loop to find every element of this object
{

if (Doca. All[i]. TagName = = "button")//If this element is a
{
HtmlElement myelement = Doca. All[i]; This element is instantiated into a HtmlElement object
if (myelement. Outertext = = "Login" && myelement. Id = = "J_submitstatic")//if the text of this element is "next page"
{
MyElement. InvokeMember ("click"); Click on this element
Break
}
}

else if (ss. Contains ("Order Information"))
{
SS = ss. Split (new string[] {"contains field"}, Stringsplitoptions.none) [1]. Split (new string[] {"contains field"}, Stringsplitoptions.none) [0];//Interception of pages, too much content
Ss. Replace ("\ t", "\ n");
StreamWriter SW = new StreamWriter ("A.txt", true, System.Text.Encoding.Unicode);
Sw. WriteLine (DateTime.Now + ":");
Sw. WriteLine (ss);
Sw. WriteLine ("---------------------------------------------------------------------------------------------------- --------------------");
Sw. Close ();
}

}

}

WebBrowser control use (belongs to the personal diary, I am also a novice)

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.