温馨提示:本文翻译自stackoverflow.com,查看原文请点击:javascript - Can you track mouse position on a webpage in Selenium webbrouser? C#
c# google-chrome-extension javascript selenium selenium-chromedriver

javascript - 您可以在Selenium webbrouser中的网页上跟踪鼠标位置吗?

发布于 2020-04-04 00:35:56

我想跟踪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)和窗口距页面底部的距离(page height - (scroll amount + window height size))之类的东西,但是JavaScript希望返回0而不是在某些特定页面位置上返回某些值。我尝试按像素滚动以找到JavaScript不会在其中返回0所需值的位置,但是它变得太不可靠了。

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

查看更多

提问者
Roman
被浏览
65
Roman 2020-02-04 07:52

因此,我创建了此类。它跟踪鼠标坐标,但有时可能会使它们弄乱(有些固定,但仍然可以)

要使用它,您必须在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);
            }
        }