Manipulate the file upload control in a Web page by simulating a keyboard mouse in WebBrowser

Source: Internet
Author: User

Manipulate the file upload control in a Web page by simulating a keyboard mouse in WebBrowser Introduction

These two days addicted to Google SketchUp, just play enough, on a whim, research a bit webbrowser.

I have mentioned in the article "WebBrowser Control using skill sharing" that "I can now manipulate all kinds of HTML elements through WebBrowser, but I can't control the HTML upload Control", for security reasons, IE does not provide manipulation support for the upload control. This makes it impossible to assign values as simple code as controlling other controls.

The actual solution is to simulate the operation, the following I will demonstrate through the keyboard, mouse two ways to simulate the click "Browse" button, and then with the keyboard simulation input file path, and press ENTER to confirm.

Initial Environment

The test uses a simple HTML page where some file upload controls are distributed in each location, some are placed directly, some are horizontally arranged, and some are nested in tables to test the triggering effect at different locations:

Load this page with the WebBrowser control.

In the program interface, I placed some controls to choose the directory where the files are uploaded, first select a directory containing the files, and then randomly select a file to fill in the upload control:

And in the code to establish an auxiliary method, to read all the file upload control on the page, in the test is also randomly extracted from one to manipulate:

list

{

var L = new list

foreach (HtmlElement f in webBrowser1.Document.GetElementsByTagName ("input"))

{

if (F.getattribute ("type") = = = "File")

{

L.add (f);

}

}

return l;

}

A random type member variable is defined in the class that is used to generate the stochastic number:

Random R = new Random ();

It also defines a series of methods for delaying 3 seconds after clicking a button, waiting for the file browse dialog to open, then simulating the input file path, and then simulating the input enter key to determine:

Void Delay Action dialog box (string fill file path)

{

Button1. Enabled = Button2. Enabled = Button3. Enabled = false;

BackgroundWorker B = new BackgroundWorker ();

b.runworkercompleted + = new Runworkercompletedeventhandler (b_runworkercompleted);

B.dowork + = new Doworkeventhandler (b_dowork);

B.runworkerasync (fill in the file path);

}

void B_dowork (object sender, DoWorkEventArgs e)

{

Thread.Sleep (3000);

E.result = e.argument;

}

void B_runworkercompleted (object sender, Runworkercompletedeventargs e)

{

Sendkeys.send (E.result as String);

Sendkeys.send ("{Enter}");

Button1. Enabled = Button2. Enabled = Button3. Enabled = true;

}

This is done by using the BackgroundWorker component to delay 3 seconds in the background, after the delay is over, and in the callback event.

keyboard emulation Mode

Watch the recorded animation first:

Here we first select a directory to upload the file, and then perform several keyboard simulation operation test.

The operating flow of the keyboard simulation is as follows:

Activate the WebBrowser control first

Then let the file upload control get the focus, and the cursor will be in the text box to the left of the file upload control

Analog input tab Toggle focus to "Browse ..." Button

Analog input SPACEBAR Click the button

Then the delay is 3 seconds to wait for the file selection dialog to display, simulate the input file path and simulate the input enter.

The main code is as follows:

private void Button1_Click (object sender, EventArgs e)

{

var L = Read Upload control ();

var s = directory.getfiles (Folderbrowserdialog1.selectedpath);

Keyboard operation (L[r.next (L.count)], S[r.next (s.length)]);

}

void keyboard operation (htmlelement element, string fill file path)

{

Webbrowser1.select ();

Webbrowser1.focus ();

Elements. Focus ();

Sendkeys.send ("{tab}");

Sendkeys.send ("");

Deferred Action dialog box (fill in the file path);

}

Mouse Simulation Mode

Or watch the recorded animation first:

The main processes of mouse simulation are:

First recursively calculates the coordinate position of the file Upload control in the page relative to the upper-left corner of the page

Then recursively calculate the position of the WebBrowser control relative to the upper-left corner of the screen

The position value is added to the control's own width and height, and is supplemented with a correction value to ensure that the mouse can point to the button

Move the mouse to the calculated position

Click the mouse

Then also delay 3 seconds wait for File selection dialog to display, simulate input file path and simulate input enter key

Recursively calculates the function of the page element relative to the position of the page's upper corner:

Point compute Coordinates (htmlelement element, point start coordinate)

{

var p = start coordinate;

P.offset (element. Offsetrectangle.location);

return element. OffsetParent = = null? P: Calculates coordinates (elements. OffsetParent, p);

}

Recursive calculation of a control's function relative to the upper-left corner of the screen:

Private point computed coordinates (control control, point start coordinate)

{

var p = start coordinate;

P.offset (Control. Location);

return control. Parent = = null? P: Calculates the coordinates (control. Parent, p);

}

In addition, to simulate mouse movement and clicks, you also need to introduce the Windows API:

[DllImport ("User32")]

public extern static void Setcursorpos (int x, int y);

[DllImport ("User32.dll")]

static extern void Mouse_event (mouseeventflag flags, int dx, int dy, uint data, int extraInfo);

[Flags]

Enum Mouseeventflag:uint

{

Move = 0x0001,

Leftdown = 0x0002,

Leftup = 0x0004,

Rightdown = 0x0008,

Rightup = 0x0010,

Middledown = 0x0020,

Middleup = 0x0040,

Xdown = 0x0080,

XUp = 0x0100,

Wheel = 0x0800,

Virtualdesk = 0x4000,

Absolute = 0x8000

}

The main code is as follows:

private void Button2_Click (object sender, EventArgs e)

{

var L = Read Upload control ();

var s = directory.getfiles (Folderbrowserdialog1.selectedpath);

Mouse operation (L[r.next (L.count)], S[r.next (s.length)]);

}

void mouse action (htmlelement element, string fill file path)

{

var p = Computed coordinates (element, new Point ());

p = Computed coordinates (webBrowser1, p);

P.offset (element. Offsetrectangle.width-5, Element. Offsetrectangle.height + 15);

Setcursorpos (p.x, P.Y);

Mouse_event (Mouseeventflag.leftdown | Mouseeventflag.leftup, 0, 0, 0, 0);

Deferred Action dialog box (fill in the file path);

}

Summary

It is recommended to use the keyboard simulation method in both methods, simple and direct, the mouse simulation method needs to use to the API, but also need to calculate accurately, and if the page with scroll bar, and scroll bar scroll or the file upload control is outside the visual area, can not be calculated and manipulated by the above method, And its advantages are just some intuitive.

Download the sample source code for this article: http://www.uushare.com/user/icesee/file/1869210

Download the XPS version of this article: http://www.uushare.com/user/icesee/file/1869213

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.