使用Selenium WebDriver获取网页的屏幕位置

使用Selenium WebDriver获取网页的屏幕位置,第1张

概述有没有办法通过Selenium WebDriver获取 HTML窗口(页面主体)的屏幕坐标? 看了几次,还没有找到WebDriver的优雅解决方案(他们有一个看起来支持他们的ILocatable设置的参数,但该方法尚未实现). 我所做的是使用UIAutomation获取windows AutomationElement并使用树木行者来查找窗口的实际对象 – 缺点是我注意到浏览器偶尔会更新它们的窗口 有没有办法通过Selenium WebDriver获取 HTML窗口(页面主体)的屏幕坐标?解决方法 看了几次,还没有找到WebDriver的优雅解决方案(他们有一个看起来支持他们的ILocatable设置的参数,但该方法尚未实现).

我所做的是使用UIautomation获取windows automationElement并使用树木行者来查找窗口的实际对象 – 缺点是我注意到浏览器偶尔会更新它们的窗口,因此条件必须每隔一段时间更改一次以适应.

这是一些示例代码(我在这里删除了一些公司代码,所以它在我的结尾更优雅,但这应该适用于C#)

public static Rectangle GetAbsCoordinates(this IWebElement element)    {        var driver = GetDriver(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,"Google Chrome"));                    if (doc == null)                        throw new Exception("unable to find element containing browser window");                    doc = doc.FindFirst(TreeScope.Descendants,ControlType.document));                }                break;            case "internet explorer":                doc = ae.FindFirst(TreeScope.Descendants,new AndCondition(new PropertyCondition(automationElement.ControlTypeProperty,"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 IWebDriver GetDriver(this IWebElement e)    {        return ((IWrapsDriver)e).WrappedDriver;    }    public static IntPtr GetIntPtrHandle(this IWebDriver driver,int timeoutSeconds = Timeout)    {        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;    }
总结

以上是内存溢出为你收集整理的使用Selenium WebDriver获取网页的屏幕位置全部内容,希望文章能够帮你解决使用Selenium WebDriver获取网页的屏幕位置所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1139098.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存