我正在尝试在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
的位置,但它变得太不可靠了。
我真的希望在这里找到一些帮助。提前感谢!
所以我创建了这个类。它会跟踪鼠标坐标,但有时它会将它们弄乱一点(有点固定,它仍然可以(。
要使用它,您必须在加载新网页时调用NewPage()
,但只有在完全加载后。然后,您可以使用MoveTo()
,传递x, y
坐标或IWebElement
将光标移动到其位置。如果需要将页面滚动y
像素,可以使用Scroll()
。您可以单击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);
}
}