Some experience of browser Automation 3 WebBrowser control's fragmentation problem

Source: Internet
Author: User

1. It is generally necessary to implement this sentence: Webbrowser1.scripterrorssuppressed = true;

The main goal is to disable the JavaScript error dialog box, otherwise it will cause the program not to run correctly. The downside is clearly that some real JavaScript errors are obscured. But in practice it is felt that in most cases this sentence is necessary.

2. Parsing the DOM

The WebBrowser control provides some methods for parsing the DOM, such as WebBrowser1.Document.GetElementById (). These methods are inconvenient when dealing with some of the more complex DOM, and I generally like to use the Htmlagilitypack library to parse the DOM.

Htmlagilitypack is very powerful, but one big problem is that when the WebBrowser method finds the desired element, it can perform the click and so on, and Htmlagilitypack does not (because it cannot execute JavaScript). So when I need the click and so on, I generally use the method provided by the Webbrower control, or simply swap with Selenium's webdriver (discussed later).

3. Save the picture

There are two main methods (if the paste 5th "Save As" also counted, can say there are 3 ways), one is to parse the DOM first, get the image URL, and then send httprequest/with webclient download Image:

https://social.msdn.microsoft.com/Forums/en-US/3cce6004-2b04-46f4-ac2e-b16a9852d62d/ How-to-save-imagepicture-from-webbrowser-control?forum=ieextensiondevelopment

The main problem is that some websites require cookies or other headers, and this approach is cumbersome.

Another way is to use the Clipboard:

https://stackoverflow.com/questions/3833718/webbrowser-copy-image-to-clipboard/3833846#3833846

The main problem encountered in practice is that it is difficult to preserve the format of the image, compared to the original PNG format, after saving, regardless

Bitmap bmp = (Bitmap) clipboard.getdataobject (). The DataFormat in GetData (Dataformats.bitmap) cannot be saved in PNG format (see the size of the saved file and the size of the file saved directly from the "Save as" in the Web page). This question does not know how to solve. I was taught by Gaoming.

4. Session Issues

In WinForm If you use multiple TabPage WebBrowser controls to open the same URL at the same time, if the site uses a session, then the WebBrowser controls in these TabPage will "share" the same session. This is a bit of a pit dad. The following code is used to illustrate:

The code in WinForm:

Private voidWinweb_load (Objectsender, EventArgs e) {//Winweb is a WinForm, placed in a TabPage, contains a WebBrowser control, which in the Load event navigate            stringURL ="http://abc.com?a=test"; intCount = Util.getcount ();//the current maximum count value is recorded in the Util classcount++;//every call (i.e. a new TabPage) plus 1URL + ="&count="+ count. ToString ();//to a Web page to distinguish between sessions of different tabpage.webbrowser1.navigate (URL);}

The code in the Web page (ASP. WebForm):

protected voidPage_Load (Objectsender, EventArgs e) {                    stringCount = request.querystring["Count"]; if(String.IsNullOrEmpty (count)) {count="1"; } viewstate["Count"] = count;//save in ViewState, because the rest of the page is also used                    intPID = Convert.ToInt32 (request.querystring["PID"]); session[viewstate["Count"]. ToString () +"selectedoid"] = PID;//Create a session, use the count value to implement different TabPage open pages have different sessions, or "shared" session                   }

5. "Save As"

How do I use code to implement a browser right-click, then click "Save Picture as" behavior? If you are using the WebBrowser control, there seems to be no good way to estimate the screen coordinates, and then simulate the mouse behavior, here to post a paragraph although work (for the Web page in the program, if used in other pages, obviously to do a little modification), but obviously very clumsy code:

Private voidWebbrowser1_documentcompleted (Objectsender, WebBrowserDocumentCompletedEventArgs e) {timer1.                Stop (); Timer1.        Start ();} Private voidTimer1_Tick (Objectsender, EventArgs e) {timer1.            Stop (); Point Controlloc= This.            Pointtoscreen (webbrowser1.location); controlloc.x= controlloc.x + webBrowser1.Document.GetElementById ("Memu6"). Offsetrectangle.left + $;//Find the right mouse click positionCONTROLLOC.Y = Controlloc.y + webBrowser1.Document.GetElementById ("Memu6"). Offsetrectangle.top + Max; Cursor.position=Controlloc; Mouseandkeysimulator.clickrightmousebutton ();//Mouseandkeysimulator, as the name implies, is a third-party class that simulates mouse and keyboard timeTimer2. Stop ();//because it is analog click, need to have a certain delay, here with the timer implementationTimer2.        Start (); }        Private voidTimer2_tick (Objectsender, EventArgs e) {Timer2.            Stop (); varNEWX = cursor.position.x +Ten; Point Newpoint=NewPoint (); Newpoint.x=newx; Newpoint.y=CURSOR.POSITION.Y; Cursor.position=Newpoint; Mouseandkeysimulator.presskey (KEYS.G,true); Mouseandkeysimulator.presskey (KEYS.G,false);//simulate keyboard action, move down to the Picture menu item             for(inti =0; I <3; i++)            {//simulate keyboard action and move down to the Save Picture as menu itemMouseandkeysimulator.presskey (Keys.up,true); Mouseandkeysimulator.presskey (Keys.up,false); } mouseandkeysimulator.presskey (Keys.enter,true);//Open the Save Picture dialog boxMouseandkeysimulator.presskey (Keys.enter,false); Timer3. Stop ();//Opening a dialog box also requires a delayTimer3.        Start (); }        Private voidTimer3_tick (Objectsender, EventArgs e) {Timer3.            Stop (); Mouseandkeysimulator.presskey (Keys.enter,true);//Save the picture with the default file name, and if you want to save it, you have to simulate the keyboard input file nameMouseandkeysimulator.presskey (Keys.enter,false); Timer4. Stop ();//this is to handle the next picture, the code here withheldTimer4.        Start (); }

When you discuss Selenium's webdriver later, you may also refer to similar functions, and you will find it much easier to implement them. The WebBrowser control does not provide any support for simulating mouse and keyboard behavior, which is difficult to implement.

Some experience of browser Automation 3 WebBrowser control's fragmentation problem

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.