Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the position of an element in the Web Browser Control?

I have a form with web browser control, which loads a webpage ( Its working fine, the page loads ok)

Now my issue is, i want to find whether a particular url-link is below the fold or above the fold ( i mean, whether the user have to scroll down to see this link, or not ) whether this v is visible with out scrolling or we need to scroll to see it.. I hope i am clear

i have done extensive search, but looks like no info is available about find an html elements position (above or below current view)

Does anybody knows something about this and can point me in right direction please? (i am looking for c# solution - WinForms )

Update: Big thanks to John Koerner for the code. Really appreciate the time and effort he put in solving my issue.

And to Jonathan & everybody else also.. I wish i could mark Jonathans reply also as answer, but it allows only one reply to be marked as answer. His comment was also clear and useful hint. THanks you guys are great!!!

like image 634
ugly hand Avatar asked Feb 09 '12 19:02

ugly hand


People also ask

How do I get the position of an element on my screen?

Use the Element. getBoundingClientRect() Function to Get the Position of an Element in JavaScript. The getBoundingClientRect() function produces a DOMRect object containing information about an element's size and position in the viewport.

How do you find the coordinates of a website?

In order to find the coordinates of the top left corner of the HTML page, you can use the web browser's instance properties DisplayRectangleX and DisplayRectangleY. For example, after storing a browser's instance into the variable %Browser%, then %Browser. DisplayRectangleX% will return the X dimension and %Browser.


2 Answers

Ok, I have tested this on google and stackoverflow and it seems to work:

private bool isElementVisible(WebBrowser web, string elementID)
{

    var element = web.Document.All[elementID];

    if (element == null)
        throw new ArgumentException(elementID + " did not return an object from the webbrowser");

    // Calculate the offset of the element, all the way up through the parent nodes
    var parent = element.OffsetParent;
    int xoff = element.OffsetRectangle.X;
    int yoff = element.OffsetRectangle.Y;

    while (parent != null)
    {
        xoff += parent.OffsetRectangle.X;
        yoff += parent.OffsetRectangle.Y;
        parent = parent.OffsetParent;
    }

    // Get the scrollbar offsets
    int scrollBarYPosition = web.Document.GetElementsByTagName("HTML")[0].ScrollTop;
    int scrollBarXPosition = web.Document.GetElementsByTagName("HTML")[0].ScrollLeft;

    // Calculate the visible page space
    Rectangle visibleWindow = new Rectangle(scrollBarXPosition, scrollBarYPosition, web.Width, web.Height);

    // Calculate the visible area of the element
    Rectangle elementWindow = new Rectangle(xoff,yoff,element.ClientRectangle.Width, element.ClientRectangle.Height);

    if (visibleWindow.IntersectsWith(elementWindow))
    {
        return true;
    }
    else
    {
        return false;
    }
}

Then to use it, you simply call:

isElementVisible(webBrowser1, "topbar")  //StackOverflow's top navigation bar
like image 70
John Koerner Avatar answered Sep 18 '22 16:09

John Koerner


I have and idea that could work (never tried it, but it's the best that I can offer to you, sorry)

You can call javascripts functions in the webbrowsercontrol: LINK

You can also make javascripts functions that give you the position of an element: LINK

If you mix this two concepts, you can know if the element is visible or not, as you know the size of the webbrowsercontrol.

Note that you can inject javascript code to the webbrowsercontrol. This SO post explain how to do it: LINK

Good luck.

like image 32
Jonathan Avatar answered Sep 19 '22 16:09

Jonathan