Coffee Guy
Coffee Guy

Reputation: 155

How to get screen coordinate of a element in selenium? [Python]

I wanted to know that how can one get the coordinates of a element according to the screen resolution rather than the browser windows size, I have tried this already (code block), but it provides coordinates according to the browser window rather than the screen

element = driver.find_element_by_xpath("//*[@id='search_form_input_homepage']")
print(element.location)

Any alternatives that I can use?

A terrible attempt to explain what I mean :

note: driver.execute_script is not allowed, as the website has a bot blocker :( This is the visual representation of what im trying to say

Upvotes: 0

Views: 2907

Answers (2)

The Pilot Dude
The Pilot Dude

Reputation: 2227

You can use .size and .location to get the sizes.

Try this:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep, strftime

url = "some url"

webdriver = webdriver.Chrome()
webdriver.get(url)


webdriver.fullscreen_window()

cookies = webdriver.find_element_by_xpath("xome xpath")

location = cookies.location
size = cookies.size
w, h = size['width'], size['height']

print(location)
print(size)
print(w, h)

Upvotes: 2

PDHide
PDHide

Reputation: 19979

print(element.location_once_scrolled_into_view)

Try if this helps , more available methods like size rect etc can be found at:

https://www.selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webelement.html#module-selenium.webdriver.remote.webelement

Upvotes: 1

Related Questions