Find the x & y coordinates in a webpage


I am working for QA Automation. We are using VBScript in our automation tool. I have a problem.

I need to capture the x and y co-ordinates of the point where the current focus is. For example if a node of a tree has been selected and the focus is on it. I need to findout the x and y co-ordinates of that node. Will this be possible in VBSCript. Can anyone help me out.

Note : I need a VBScript code which will get me the coordinates of my cursor position.

Thanks in advance.
Who is Participating?
moduloConnect With a Mentor Commented:
PAQed with points refunded (500)

Community Support Moderator
use the mouseevent  onMouseOver on a any particular object your trying to find then
use the following in a js routine

var mousex = window.event.x;        // mouse location capture event
var mousey = window.event.y;        // mouse location capture event

sangackAuthor Commented:
I am using keyboard Down key to navigate to the specified node of a Tree control. Now my cursor is highlighted in that position, my mouse remains at different position.

I just want to get my cursor coordinates so that I will take my mouse to the cursor position using my Automation Tool.

How to get the cursor coordinates???

Note : I need a VBScript code which will get me the coordinates of my cursor position.


I can use user32.dll and get the x y co ordinates.

extern.declare micHwnd, "GetCursorPos", "user32.dll", "GetCursorPos"

GetCursorPos(LPOINT lpPoint)

But this LPOINT is giving me Error

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

This might be a little better

fields T1 and T2  ( t1 and t2 repectively in the javascript)
are purely for displaying the values

<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 3</title>
<script language = "javascript">
      function getMousePos(){
            var t1 = document.getElementById("T1")
            var t2 = document.getElementById("T2")

            var mousex = window.event.x;        // mouse location capture event
            var mousey = window.event.y;        // mouse location capture event

            t1.value = mousex
            t2.value = mousey

 document.onmousemove = getMousePos;


<input type="text" id="T1" name="T1" size="20">
<input type="text" id="T2" name="T2" size="20">


sangackAuthor Commented:
My mouse remains idle at different position, only my cursor moves.

I need the cursor coordinates????
apologies,  assumed mouse as being cursor
could you not do something very basic with keypress events    if left  x-1,  right x+1   up  y-1  down y+1?

sangackAuthor Commented:
See I have a code in Visual basic which will talk to user32.dll and get the co ordinates.

Here it is :

Private Declare Sub GetCursorPos Lib "user32.dll" (lpPoint As POINTAPI)

Private Type POINTAPI
X As Integer
Y As Integer
End Type
Private mPosition As POINTAPI

Call GetCursorPos(mPosition)
msgbox mPosition.X
msgbox mPosition.Y

But I want this to work in a VBSCRIPT file. ie vbs file. If you could make it happen, appreciated!!!
why not create  a vb  dll  and then refer to it as a seperate function

look on  4guysfromrolla   for information about formatting  ( i know it 's not )   but they explain how to creat a dll in vb nd then referencing it through asp,  I use it in mine at work,   but don't have the code here,  and me baby is lying asleep in my arms, so typing one handed

sangackAuthor Commented:
Your sugession is good, but can't implement as I need to find the solution in VBSCRIPT only.

Any help in building in VBSCRIPT appreciated!!!
sangackAuthor Commented:
Here is the VB code which I need.

For your reference....

*********************************************************************Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type

    cbSize As Long
    flags As Long
    hwndActive As Long
    hwndFocus As Long
    hwndCapture As Long
    hwndMenuOwner As Long
    hwndMoveSize As Long
    hwndCaret As Long
    rcCaret As RECT
End Type

Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetFocus Lib "user32" () As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetGUIThreadInfo Lib "user32" (ByVal dwthreadid As Long, lpguithreadinfo As GUITHREADINFO) As Long

Public Sub GetFocusedControlPos(ByRef x As Integer, ByRef y As Integer)
   Dim WndHandle As Long
   Dim SelectedPID As Long
   Dim MyRect As RECT
   Dim lpClassName As String
   'Get active window
   WndHandle = GetForegroundWindow()
   'Get GUI PID of the selected thread
   SelectedPID = GetWindowThreadProcessId(WndHandle, 0)

   RetInfo.cbSize = LenB(RetInfo)
   If GetGUIThreadInfo(SelectedPID, RetInfo) = 0 Then
        MsgBox "Error while calling the function!"
        Exit Sub
    End If
     'Get window rectangle
   GetWindowRect RetInfo.hwndFocus, MyRect

    lpClassName = Space(256)
    'retrieve the class name
    RetVal = GetClassName(RetInfo.hwndFocus, lpClassName, 256)

    MsgBox lpClassName
   x = MyRect.Left
   y = MyRect.Top
End Sub

Private Sub Timer1_Timer()
   Dim x As Integer
   Dim y As Integer

   GetFocusedControlPos x, y
   Me.Caption = x & " " & y
End Sub

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.