Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the on-screen location of web page with Selenium WebDriver

Is there a way to get the on-screen coordinates of HTML window (page body) with Selenium WebDriver?

like image 526
al0 Avatar asked Dec 24 '13 13:12

al0


People also ask

How do you get the location in Selenium?

To get the position or coordinates of a web element using Selenium in Java, call getLocation() on the Web Element object.

How do you find the position of WebElement?

getLocation(). getX(); has been used to find the location and x coordinate of the location of Gmail WebElement. 2. getY() method with a combination of getLocation() has been used to find y coordinate of the location of Gmail WebElement.

How do I 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.

How would you get the coordinates of an element in Selenium?

Selenium executes JavaScript commands with the help of the executeScript method. To get the unique coordinates of an element we shall create an object of the class Point which shall store the location of the webelement obtained from the getLocation method.


1 Answers

The posted code by Zechtitus is amazing, I tried it under IE11 and Chrome Version 39.0.2171.95 m and it worked like a charm. Although I had to pass the real object of IWebDriver instead of using WrappedDriver because it doesn't work with Chrome. Just for your info, I have Win 7 ultimate x64 and using Selenium WebDriver 2.44. this is the code that I took it from Zechtitus and modified it:

    public static Rectangle GetAbsCoordinates(IWebDriver driver, IWebElement element)
    {
        var handle = GetIntPtrHandle(driver);
        var ae = AutomationElement.FromHandle(handle);
        AutomationElement doc = null;
        var caps = ((RemoteWebDriver)driver).Capabilities;
        var browserName = caps.BrowserName;
        switch (browserName)
        {
            case "safari":
                var conditions = (new AndCondition(new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Pane),
                    new PropertyCondition(AutomationElement.ClassNameProperty, "SearchableWebView")));
                doc = ae.FindFirst(TreeScope.Descendants, conditions);
                break;
            case "firefox":
                doc = ae.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document));
                break;
            case "chrome":
                doc = ae.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Chrome Legacy Window"));
                if (doc == null)
                {
                    doc = ae.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Google Chrome"));
                    if (doc == null)
                        throw new Exception("unable to find element containing browser window");
                    doc = doc.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document));
                }
                break;
            case "internet explorer":
                doc = ae.FindFirst(TreeScope.Descendants, new AndCondition(new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Pane),
                    new PropertyCondition(AutomationElement.ClassNameProperty, "TabWindowClass")));
                break;
        }

        if (doc == null)
            throw new Exception("unable to find element containing browser window");

        var iWinLeft = (int)doc.Current.BoundingRectangle.Left;
        var iWinTop = (int)doc.Current.BoundingRectangle.Top;

        var coords = ((ILocatable)element).Coordinates;
        var rect = new Rectangle(iWinLeft + coords.LocationInDom.X, iWinTop + coords.LocationInDom.Y, element.Size.Width, element.Size.Height);
        return rect;
    }

    public static IntPtr GetIntPtrHandle(this IWebDriver driver, int timeoutSeconds = 20)
    {
        var end = DateTime.Now.AddSeconds(timeoutSeconds);
        while (DateTime.Now < end)
        {
            // Searching by AutomationElement is a bit faster (can filter by children only)
            var ele = AutomationElement.RootElement;
            foreach (AutomationElement child in ele.FindAll(TreeScope.Children, Condition.TrueCondition))
            {
                if (!child.Current.Name.Contains(driver.Title)) continue;
                return new IntPtr(child.Current.NativeWindowHandle); ;
            }
        }
        return IntPtr.Zero;
    }

and I used it like this:

Rectangle recView = GetAbsCoordinates(MyWebDriverObj, myIWebElementObj);

the correct X, Y are then stored in recView.X and recView.Y As I said, it's working for me for both IE11 and Chrome. Good luck

like image 121
Akram Alhinnawi Avatar answered Oct 26 '22 16:10

Akram Alhinnawi