Find the x & y coordinates in a webpage

Posted on 2004-09-03
Last Modified: 2013-12-16

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.
Question by:sangack
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5

Expert Comment

ID: 11979300
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


Author Comment

ID: 11979316
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


Expert Comment

ID: 11979324
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">


NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!


Author Comment

ID: 11979338
My mouse remains idle at different position, only my cursor moves.

I need the cursor coordinates????

Expert Comment

ID: 11979356
apologies,  assumed mouse as being cursor

Expert Comment

ID: 11979378
could you not do something very basic with keypress events    if left  x-1,  right x+1   up  y-1  down y+1?


Author Comment

ID: 11979406
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!!!

Expert Comment

ID: 11979437
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


Author Comment

ID: 11987101
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!!!

Author Comment

ID: 12023850
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


Accepted Solution

modulo earned 0 total points
ID: 12743120
PAQed with points refunded (500)

Community Support Moderator

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL to update characters in table column 6 163
Load data upon clicking a button 8 48
Designing forms 3 45
SP result not being displayed 5 46
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This article provides a case study on how our local youth baseball league deployed a new website, including the platform selection, implementation and benefits to the league.
The purpose of this video is to demonstrate how to properly insert a Vimeo Video into a WordPress site or Blog. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following:…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question