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
  • 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">


What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.


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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Autosum input type=text when checkbox is true 28 96
I starting with php 12 133
ASP Button to clear text 4 56
The Best Website Creation Tools 14 56
In this short web based tutorial, I wanted to show users how they can still use the powers of FrontPage in conjunction with Expression Web 3.  Even though Microsoft eliminated the use of Web components, we can still use them with FrontPage and edit …
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
The purpose of this video is to demonstrate how to make a WordPress Site faster and smaller in size by cleaning up the database. This will be demonstrated using a Windows 8 PC. Plugin WP Optimize will be used. Go to your WordPress login page. T…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …

809 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