Executing Javascript elementFromPoint through Selenium driver

前端 未结 3 1982
暗喜
暗喜 2021-01-15 02:28

I am trying to implement an \"object picker\" to my Selenium based framework as is common on most commercial automation tools. To do this I am using a Javascript command to

3条回答
  •  天命终不由人
    2021-01-15 03:17

    It's actually about how you are passing the coordinates into the script. Script arguments has to be specified separately as separate ExecuteScript() arguments. What was happening in your case is that you have basically specified one x argument which made it think that y should be considered a default 0 value. And at y=0 there is usually a header.

    Instead of:

    ele = (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript(
                            "return document.elementFromPoint(arguments[0], arguments[1])", 
                            new int[] { Cursor.Position.X, Cursor.Position.Y });
    

    You should do:

    ele = (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript(
                            "return document.elementFromPoint(arguments[0], arguments[1])", 
                            Cursor.Position.X, Cursor.Position.Y);
    

提交回复
热议问题