C # Gets the location of an element in the page and simulates the click

Source: Internet
Author: User

In development, we tend to get the location of an element in the page and click on it. To simulate a mouse click is not difficult, as long as the call an API on the line, the key is how to get the position of this element, as well as determine whether to scroll the scroll bar, how many rows to scroll to let the element display. Of course, we can dynamically change its CSS to show it in a specific location, but this method is only valid for relatively simple pages.

So how can we get the location of the page, first we look at a picture

From here we can see the properties of five offset, here we mainly use offsetparent, offsetleft and offsettop, we use offsetparent to get the element offset of the parent element, and then loop until the body.

First we quote windows\system32\ MSHTML.LTB this file, so that we can get some special features, the function of this library is particularly powerful, if you want to do HTML editor, you can use this library and WebBrowser combination, made out of the editor function is very powerful, that is, the code is a bit incomplete web standards. Then we want the using MSHTML, so the following code will work.

Code:

HTMLDocument doc = webBrowser1.Document.DomDocument as HTMLDocument;
Getelementsbyname,getelementbyid can also use these two methods here
IHTMLElementCollection els = Doc.getelementsbytagname ("a");
Point P = new Point ();
foreach (IHTMLElement em in Els)
{
if ((Em.getattribute ("href"). ToString () = = "Javascript:fgoto ()") && (Em.innertext = = "Add Attachment"))
{
IHTMLElement PEM = em;
Element Middle
p.x = EM.OFFSETWIDTH/2;
P.Y = EM.OFFSETHEIGHT/2;
Do
{
PEM = pem.offsetparent;
p.x + = Pem.offsetleft;
P.y + = Pem.offsettop;
} while (Pem.tagName.ToLower ()! = "Body");
Em.scrollintoview ();//Display element
Break
}
}

So we've got the location of the vegan, and it's already shown in the browser's visible area, it seems that we can use the API to simulate the click, but when you test, you find that this is not the case. Why, then look down

This coordinate is screen coordinates, starting from the upper left corner of the screen, sometimes your browser is not maximized, even if it is maximized is not necessarily only webbrowser this control, even if only the control, the form of the border and so on, it is possible that your mouse does not move in the correct position. And, if the page has scrolling, we're going to have to scroll to that part as well.

If you want to click, you must have the following code:

The part that was rolled away
int SL, ST;
SL = Int. Parse (Doc.documentElement.getAttribute ("ScrollLeft"). ToString ());
st = Int. Parse (Doc.documentElement.getAttribute ("ScrollTop"). ToString ());
Plus the position of the form and the position of the control and the form border, 30 and 8 are the form borders
p.x + = Em.offsetleft + this. Left + WEBBROWSER1.LEFT-SL + 8;
P.y + = Em.offsettop + this. Top + webbrowser1.top + 30-st;
Positioning the mouse
Cursor.position = p;
Click
Mouse_event (6, 0, 0, 0, this. Handle);

So we can click on the element we need to click. About mouse_event this API please go to MSDN my online tutorials. Cursor.position This mouse positioning can also use the API function Setcursorpos, but C # has this thing does not have to call.

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.