I would like to know the on-screen position of some element. I know how to get the position of an element in Python Selenium webriver but how to get an offset from the left-top corner of the screen?
Asked
Active
Viewed 1.8k times
9
-
You need just a length of the line instead of `x`/`y` coordinates, right? – Andersson Mar 15 '17 at 11:24
-
No, i would like to know know the x,y cords of left-top html page corner that i could add to this result of webdriver.location to get on-screen location of some element. – szmo Mar 15 '17 at 11:42
4 Answers
11
I guess it's not possible to define distance from top-left corner of browser window to top-level corner of screen with just selenium
. But you can try to implement following:
driver = webdriver.Chrome()
driver.maximize_window() # now screen top-left corner == browser top-left corner
driver.get("http://stackoverflow.com/questions")
question = driver.find_element_by_link_text("Questions")
y_relative_coord = question.location['y']
browser_navigation_panel_height = driver.execute_script('return window.outerHeight - window.innerHeight;')
y_absolute_coord = y_relative_coord + browser_navigation_panel_height
x_absolute_coord = question.location['x']

Andersson
- 51,635
- 17
- 77
- 129
-
It only works if the window is maximised. Else there is a 8px margin (left right and bottom) with chrome. – Jerome Sep 19 '18 at 13:26
-
1This does not consider the window position or whether the document has been scrolled. Please see my answer. – wolfmanstout Dec 15 '19 at 19:08
11
There is no way to do this with 100% accuracy, but here is the best workaround that considers the browser window's offset, the toolbars in the window, and the scrolling position of the document:
# Assume there is equal amount of browser chrome on the left and right sides of the screen.
canvas_x_offset = driver.execute_script("return window.screenX + (window.outerWidth - window.innerWidth) / 2 - window.scrollX;")
# Assume all the browser chrome is on the top of the screen and none on the bottom.
canvas_y_offset = driver.execute_script("return window.screenY + (window.outerHeight - window.innerHeight) - window.scrollY;")
# Get the element center.
element_location = (element.rect["x"] + canvas_x_offset + element.rect["width"] / 2,
element.rect["y"] + canvas_y_offset + element.rect["height"] / 2)

wolfmanstout
- 171
- 2
- 3
-
-
1@RajatAgrawal I guess it depends on what you are trying to achieve? On the original question, there is a comment saying "i would like to know know the x,y cords of left-top html page corner that i could add to this result of webdriver.location to get on-screen location of some element." That's what I'm solving for. If you just want the distance to the top left corner, ignoring the scroll distance, just remove "- window.scrollX" and "- window.scrollY" from my code. – wolfmanstout Mar 26 '22 at 22:42
-
I want the coordinates of the element which is down the page(scrolling required) and I need to get the coordinates of that which are relative to the screen( with respect to the screen, not browser). And thanks for replying. – Rajat Agrawal Mar 28 '22 at 08:25
-
Your solution worked! But one change needed is that my laptop was running in 200% zoom mode. Needed to change it from the window's settings. So, this is something to take into considartion – Yusufbek Oct 25 '22 at 21:00
1
The method get_window_position()
of Selenium webdriver enables you to access the location of your browser with respect to your screen:
#Get the current location of your browser
browser_location = driver.get_window_position()
# eg: {'y': 127, 'x': 15}
# Set the absolute position of your Web element here (top-left corner)
element_location = (element.location["x"]+ browser_location["x"],
element.location["y"]+ browser_location["y"])

Arghya Sadhu
- 41,002
- 9
- 78
- 107

Etienne Ekpo
- 11
- 2
1
page_offset = self.driver.execute_script("return window.pageYOffset;")
ih = self.driver.execute_script("return window.innerHeight;")
oh = self.driver.execute_script("return window.outerHeight;")
chrome_offset = window_size[1]-ih
ex = pos["x"]+8+element_size["width"]/2
ey = (pos["y"]-page_offset)+chrome_offset-(element_size["height"]/2)
pyautogui.moveTo(ex,ey,2)

Ravikirana B
- 1
- 3
-
what is window_size[1] referring to I am trying to implement this approach in java. – Rajat Agrawal Mar 16 '22 at 11:50