Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello guys
I used this code in VBA Excel to find the top left node of the element in Selenium in VBA Excel:
VB
Debug.Print "Location of X,Y = " & driver.FindElementByCss("#logo > a > img").Location.X & " , " & driver.FindElementByCss("#logo > a > img").Location.Y


After run, it Shows me:

Location of X,Y = 1203 , 653

But when i take a screenshot from the page and open it in Photoshop, the location is different. I mean when I locate the top left ofthe picture of the element in Photoshop, I see the numbers of X and Y are totally different.
I should add that the resolution of my system is 1920x1080 and 72 dpi.

So it seems the numbers in the code are in another unit and are not in pixel. So how can I have the coordinates in pixel unit?

Any ideas?

What I have tried:

The above code is what I have tried.
Posted
Updated 19-Apr-23 4:55am
v2
Comments
Dave Kreskowiak 12-Apr-23 10:12am    
I've never had the need to look at the coords of HTML elements, but I would expect the coordinates of the element will be relative to the top left corner of the page, not the screen. Is this what you're possibly seeing?
Sh.H. 12-Apr-23 16:46pm    
Well you are right. But I need to compare it as a picture of screenshot in photoshop. html uses Point, and photoshop uses Pixel. So I need to know how can I convert point to pixel, so then I use it in the screenshot in photoshop.
Dave Kreskowiak 12-Apr-23 17:59pm    
You don't. There is no conversion from pixels in the page to pixels in screen coordinates. The page can be many times longer than the screen, plus you have to take into account the browser window size, it's position, the page window and it's position, the non-client area of the browser, the menu size of the browser, and button and address bars. There is no easy conversion for this at all. Oh, and how far the page has been scrolled is going to screw all this up too.

Sh.H. 14-Apr-23 6:36am    
So why the result is different from the result in Photoshop???
Dave Kreskowiak 14-Apr-23 8:53am    
I already told you!

The coordinates you get from your code are the coordinates relative to the top left OF THE WEB PAGE, NOT THE SCREEN!

It turns out that you CAN get some of the data you need, like the nonviewport height of the browser window and it's position on screen. It helps to go back and re-read the documenation I haven't read in the last decade.
HTML
<html>
<head>
	<title>Screen Coord Test</title>
	<style>
	button {
		margin-left: 100;
	}
	</style>
</head>
<body>
<h1>Script Test</h1>

<button id="testId" type="button">Test Button</button>

<script type="text/javascript">
var element = document.getElementById('testId');
var position = element.getBoundingClientRect();
var buttonX = position.left;
var buttonY = position.top;

var windowX = window.screenX;
var windowY = window.screenY;
var nonViewportHeight = window.outerHeight - window.innerHeight;

console.log('Window coords: ', windowX,',', windowY);
console.log('NonViewport Height: ', nonViewportHeight);
console.log('Button CSS coords: ', buttonX,',', buttonY);
console.log('Button screen coords: ', (buttonX + windowX) ,',', (buttonY + windowY + nonViewportHeight));
</script>
</body>
</html>
 
Share this answer
 
Comments
Sh.H. 15-Apr-23 11:35am    
Thanks.
But the problem is there yet!
Please kindly see the result in the picture I attached here.
https://i.postimg.cc/sxfLD5JS/ScnSht.jpg
Dave Kreskowiak 15-Apr-23 11:37am    
You have a place to start. The rest of the research is up to you.
Sh.H. 15-Apr-23 12:03pm    
Thanks but not yet a place to start! Because the numbers are different yet! I don't know the reason. But I really need to have equal numbers. :-(
Dave Kreskowiak 15-Apr-23 13:27pm    
Neither do I and I'm not hear to do your research for you. You learn nothing that way.

Welcome to the other half of writing code! DOING RESEARCH AND EXPERIMENTATION.

Here's the starting point: https://developer.mozilla.org/en-US/docs/Web/API/Window
Sh.H. 16-Apr-23 1:07am    
Look buddy! Please pay attension to the result. It says (108 , 137). BUT IT IS NOT! Kindly please check! You will see. :-(
I have never used Selenium webdriver in Excel VBA, but i have found very interesting article: How to get X Y coordinates of element in Selenium WebDriver – Maisa Solutions Blog[^]

Quote:
Any web element has Its own position on page and generally it is measured In x and y pixels and known as x y coordinates of element. X pixels means horizontal position on page from left side and Y pixels means vertical position on page from top side. You can find x y coordinates of any element.


So, if you want to get coordinates of element to the left top-corner of screen, you need to use different method. For example:
Visual Basic Procedure to Get/Set Cursor Position - Microsoft Support[^]
 
Share this answer
 
Comments
Sh.H. 21-Apr-23 6:56am    
Thanks. I read the articles. They yet doesn't solve anything.
Please kindly read again carefully my problem in here.
The articles you said, they find the coordinates in the web browser, NOT IN THE WHOLE MONITOR!!!
What I asked is finding the element in the monitor screen.
And I couldn't find any solution yet!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900