HTML   发布时间:2022-04-14  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用Selenium WebDriver获取网页的屏幕位置大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法通过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 = datetiR_297_11845@e.Now.AddSeconds(timeoutSeconds);
        while(datetiR_297_11845@e.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获取网页的屏幕位置所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。