C # Gets the position of an element of the page, and click on simulation

We develop, often to get the position of an element of the page, and click on it. To simulate a mouse click is not difficult, as long as an API call on the line, the key is how to get the position of the element, as well as to determine whether or not to scroll the scroll bar to scroll how many lines to make the elements displayed. Of course, we can dynamically change the CSS, it is displayed in a specific location, but this method is only effective for relatively simple web page.

Then how can we get it in place on the page, we first look at a picture
 

From here we can see the properties of five offset, where we mainly use offsetparent, offsetleft and offsettop, we get offsetparent parent element offset, recycling, until BODY.

First we quote windows \ system32 \ mshtml.ltb this file, so that we can get some special features, the library features particularly strong, if they do HTML editor, you can use this library and webBrowser combination, made out of editing function is very powerful, is the code a bit incomplete WEB standards. Then we have to using mshtml; so the following code to run properly.

Code:

            HTMLDocument doc = webBrowser1.Document.DomDocument as HTMLDocument;
            //getElementsByName,getElementById 这里也可以用这两个方法
            IHTMLElementCollection els = doc.getElementsByTagName("a");
            Point p = new Point();
            foreach (IHTMLElement em in els)
            {
                if ((em.getAttribute("href").ToString() == "javascript:fGoto()") && (em.innerText == "添加附件"))
                {                    
                    IHTMLElement pem = em;
                    //元素中间
                    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();//显示元素
                    break;
                }
            }

So we've got no prime location, and has been shown in the visible area of ​​the browser, and it seems we can simulate clicking the API, but when you test and found that is not the case. Why, then look down

The coordinates are screen coordinates, starting from the top left corner of the screen, sometimes your browser is not maximized, even if the form is not necessarily maximize only webBrowser this control, even if only this control, such as a window frame series, it may be your mouse does not move the correct position. Moreover, if the page has a scrolling, we have to get to that part of the scroll.

If you have to click to the following code:

            //被卷去的部分
            int sl, st;
            sl = int.Parse(doc.documentElement.getAttribute("ScrollLeft").ToString());
            st = int.Parse(doc.documentElement.getAttribute("ScrollTop").ToString());  
            //加上窗体的位置及控件的位置及窗体边框,30和8是窗体边框           
            p.X += em.offsetLeft + this.Left + webBrowser1.Left - sl + 8;              
            p.Y += em.offsetTop + this.Top + webBrowser1.Top + 30 - st;
            //定位鼠标
            Cursor.Position = p;            
            //单击
            mouse_event(6, 0, 0, 0, this.Handle);

In this way we can click to click on the elements we need. About mouse_event this API, please see my tutorial MSDN online. Cursor.Position this mouse positioning can also use the API function SetCursorPos, but C # has this thing would not have to call.

Guess you like

Origin www.cnblogs.com/soundcode/p/12583776.html