I am currently working on integrating an "object picker" into my Selenium-based framework, a feature commonly found in many commercial automation tools. I have been using a Javascript command to locate the element at the mouse's current position, but for some reason, I am not getting the expected element.
When using ChromeDriver or InternetExplorerDriver, the script always returns the header object, regardless of the web page being viewed or the mouse position. Even though it seems like the script is fetching coordinates (0, 0) instead of the actual mouse position, I have double-checked and confirmed that Cursor.Position is providing the correct values.
On the other hand, when utilizing FirefoxDriver, I encounter an exception:
"Argument 1 of Document.elementFromPoint is not a finite floating-point value. (UnexpectedJavaScriptError)"
Could someone help me identify what mistake I might be making?
private void OnHovering()
{
if (Control.ModifierKeys == System.Windows.Forms.Keys.Control)
{
IWebElement ele = null;
try
{
// Locate the element at the mouse position
if (driver is IJavaScriptExecutor)
ele = (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript(
"return document.elementFromPoint(arguments[0], arguments[1])",
new int[] { Cursor.Position.X, Cursor.Position.Y });
// Select the identified element
if (ele != null)
SelectElement(ele);
}
catch (Exception) { }
}
}
Thank you in advance!