首页 > 解决方案 > 您可以在 Selenium webbrouser 中跟踪网页上的鼠标位置吗?C#

问题描述

我正在尝试跟踪 Selenium 浏览器中的鼠标坐标。

Selenium 没有鼠标跟踪功能。它只有MoveByOffset(int x, int y)MoveToElement(IWebElement)功能。我需要设置自定义位置并使用该MoveByOffset(int x, int y)功能移动到它们。

Selenium 允许使用 JavaScript 脚本,但似乎不允许在后台运行脚本并从所述后台函数中检索值:

我们可以运行类似的东西:

document.onmousemove = function(e)
{
    var x = e.pageX;
    var y = e.pageY;
};

并从中返回值,但这似乎是不可能的(如上所述)。

我们还可以创建一个 Chrome 扩展并将脚本放入其中background.js,但我找不到从扩展中检索值并在代码中使用它的方法。这可能吗?

我还尝试在 C# 代码中跟踪鼠标移动,但它似乎并不可靠:

我们通常需要滚动页面。它可以用:js.ExecuteScript("window.scrollBy(0," + scrollDownAmountInPixels + ")");,其中 js 是IJavaScriptExecutor. 但是滚动也会移动鼠标。我们可以将 mouse-y 值更新为滚动值,但是如果我们在页面顶部并且我们向上滚动或者如果我们在页面底部并且我们向下滚动 - 鼠标将接收到错误的坐标。我们可以测试窗口到页面page height顶部的距离scroll amount(我尝试按像素滚动以找到 JavaScript 不会返回所需值的位置,但它变得太不可靠了。page height - (scroll amount + window height size)00

我真的希望在这里找到一些帮助。提前致谢!

标签: javascriptc#seleniumgoogle-chrome-extensionselenium-chromedriver

解决方案


所以我创建了这个类。它会跟踪鼠标坐标,但有时它会使它们有点混乱(有些固定,它仍然可以)

要使用它,您必须在NewPage()加载新网页时调用 ,但只有在完全加载后才能使用它。然后您可以使用MoveTo(),传递x, y坐标或IWebElement将光标移动到它的位置。Scroll()如果您需要按y像素滚动页面,则可以使用。您可以点击Click()

这是代码:

public class MouseMover
        {
            public int PageHeight { get; private set; }
            public int PageScrolled { get; private set; }
            public int WindowHeight { get; private set; }
            public int MouseXCurr { get; private set; } // current mouse x position
            public int MouseYCurr { get; private set; } // current mouse y position

            private int MouseXToMove { get; set; }
            private int MouseYToMove { get; set; }
            private ChromeDriver Chr { get; set; }
            private Actions click;

            public MouseMover(ChromeDriver chr)
            {
                MouseXCurr = 0;
                MouseYCurr = 0;
                MouseXToMove = 0;
                MouseYToMove = 0;
                Chr = chr;
                NewPage();
                click = new Actions(chr);
                click.Click().Build();
            }

            public void NewPage() // call this whenever entering a new page and it is fully loaded in
            {
                IJavaScriptExecutor js = (IJavaScriptExecutor)Chr;

                Int64.TryParse(js.ExecuteScript("" +
                "var body = document.body," +
                "    html = document.documentElement;" +
                "" +
                "var height = Math.max(body.scrollHeight, body.offsetHeight," +
                "                         html.clientHeight, html.scrollHeight, html.offsetHeight);" +
                "return height;").ToString(), out long ph);
                PageHeight = (int)ph;

                if (PageScrolled != 0)
                {
                    MouseYCurr -= PageScrolled;
                }
                PageScrolled = 0;

                Int64.TryParse(js.ExecuteScript("return window.innerHeight;").ToString(), out long wh);
                WindowHeight = (int)wh;
            }

            public void Click() // click at the current mouse position
            {
                click.Perform();
            }

            public void Scroll(int y) // scroll the page by y pixels down (negative to scroll up)
            {
                IJavaScriptExecutor js = (IJavaScriptExecutor)Chr;
                int oldScroll = PageScrolled;
                if (y > 0)
                {
                    if (PageScrolled + WindowHeight + y <= PageHeight)
                    {
                        js.ExecuteScript("window.scrollBy(0," + y + ")");
                        PageScrolled += y;

                        // sometimes the ScrollHeight gets messed up. This helps to fix it, but it doesn't always fix it
                        Int64.TryParse(js.ExecuteScript("return (window.pageYOffset || document.documentElement.scrollTop)  - (document.documentElement.clientTop || 0);").ToString(), out long s);
                        if (s != 0 && PageScrolled != (int)s)
                        {
                            PageScrolled = (int)s;
                        }

                        MouseYCurr += PageScrolled - oldScroll;
                    }
                    else
                    {
                        if (PageHeight != PageScrolled + WindowHeight)
                        {
                            js.ExecuteScript("window.scrollBy(0," + (PageHeight - (PageScrolled + WindowHeight)) + ")");
                            PageScrolled += (PageHeight - (PageScrolled + WindowHeight));

                        // sometimes the ScrollHeight gets messed up. This helps to fix it, but it doesn't always fix it
                            Int64.TryParse(js.ExecuteScript("return (window.pageYOffset || document.documentElement.scrollTop)  - (document.documentElement.clientTop || 0);").ToString(), out long s);
                            if (s != 0 && PageScrolled != (int)s)
                            {
                                PageScrolled = (int)s;
                            }

                            MouseYCurr += PageScrolled - oldScroll;
                        }
                    }
                }
                else
                {
                    if (PageScrolled >= -y)
                    {
                        js.ExecuteScript("window.scrollBy(0," + y + ")");
                        PageScrolled += y;

                        // sometimes the ScrollHeight gets messed up. This helps to fix it, but it doesn't always fix it
                        Int64.TryParse(js.ExecuteScript("return (window.pageYOffset || document.documentElement.scrollTop)  - (document.documentElement.clientTop || 0);").ToString(), out long s);
                        if (s != 0 && PageScrolled != (int)s)
                        {
                            PageScrolled = (int)s;
                        }

                        MouseYCurr += PageScrolled - oldScroll;
                    }
                    else
                    {
                        js.ExecuteScript("window.scrollBy(0," + -PageScrolled + ")");
                        PageScrolled -= PageScrolled;

                        // sometimes the ScrollHeight gets messed up. This helps to fix it, but it doesn't always fix it
                        Int64.TryParse(js.ExecuteScript("return (window.pageYOffset || document.documentElement.scrollTop)  - (document.documentElement.clientTop || 0);").ToString(), out long s);
                        if (s != 0 && PageScrolled != (int)s)
                        {
                            PageScrolled = (int)s;
                        }

                        MouseYCurr += PageScrolled - oldScroll;
                    }
                }
            }

            public void MoveTo(IWebElement el) // move to the middle of the given web element
            {
                MoveTo(el.Location.X + el.Size.Width / 2, el.Location.Y + el.Size.Height / 2);
            }

            public void MoveTo(int x, int y) // move to the given page coordinates
            {
                MouseXToMove = x - MouseXCurr;
                MouseYToMove = y - MouseYCurr;
                Move();
            }

            void Move()
            {
                bool retry;
                do
                {
                    try
                    {
                        retry = false;
                        Actions actions = new Actions(Chr);
                        actions.MoveByOffset(MouseXToMove, MouseYToMove).Build().Perform();

                        // this will only be executed when the target coordinates enter the screen
                        MouseXCurr += MouseXToMove;
                        MouseYCurr += MouseYToMove;
                        MouseXToMove = 0;
                        MouseYToMove = 0;
                    }
                    catch
                    {
                        retry = true;
                        if (MouseYToMove > 0)
                        {
                            int oldScroll = PageScrolled;
                            Scroll(50);
                            MouseYToMove -= PageScrolled - oldScroll;
                        }
                        else
                        {
                            int oldScroll = PageScrolled;
                            Scroll(-50);
                            MouseYToMove -= PageScrolled - oldScroll;
                        }
                    }
                }
                while (retry == true);
            }
        }

推荐阅读