Search code examples
dotnetbrowser

How to get the absolute coordinate of a specific elements in DotNetBrowser?


Anyone know how to get the absolute coordinate of a specific elements using DotNetBrowser?

Thankyou!!


Solution

  • You could put together absolute coordinates of a window, a viewport and an element. To get a screen resolution you could use Screen.PrimaryScreen.Bounds property

    Here is a code sample:

    public partial class Form1 : Form
    {
        BrowserView browserView;
    
        public Form1()
        {
            InitializeComponent();
            browserView = new WinFormsBrowserView();
    
            browserView.Browser.FinishLoadingFrameEvent += Browser_FinishLoadingFrameEvent;
            Controls.Add((Control)browserView);
            browserView.Browser.LoadURL("google.com");
        }
    
        private void Browser_FinishLoadingFrameEvent(object sender, DotNetBrowser.Events.FinishLoadingEventArgs e)
        {
            if (e.IsMainFrame)
            {
                DOMDocument document = e.Browser.GetDocument();
                DOMElement element = document.GetElementByName("btnK");
                Rectangle rectangle = element.BoundingClientRect;
    
                Rectangle resolution = Screen.PrimaryScreen.Bounds;
                Debug.WriteLine("Screen resolution = " + resolution.Width +
                   'x' + resolution.Height);
    
                Debug.WriteLine("Form X = " + Location.X);
                Debug.WriteLine("Form Y = " + Location.Y);
                Debug.WriteLine("browserView X = " + ((Control)browserView).Location.X);
                Debug.WriteLine("browserView Y = " + ((Control)browserView).Location.Y);
                Debug.WriteLine("X = " + rectangle.Location.X);
                Debug.WriteLine("Y = " + rectangle.Location.Y);
    
                int absoluteX = Location.X + 
                   ((Control)browserView).Location.X + rectangle.Location.X;
                int absoluteY = Location.Y + 
                   ((Control)browserView).Location.Y + rectangle.Location.Y;
    
                Debug.WriteLine("Absolute X = " + absoluteX);
                Debug.WriteLine("Absolute Y = " + absoluteY);
            }
        }
    }