0

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

Coffee Guy
  • 145
  • 1
  • 1
  • 8
  • 1
    Similar question: [By Andersson] https://stackoverflow.com/questions/42807676/pythonselenium-on-screen-position-of-element) – Dum Dum Jan 19 '21 at 11:45
  • Sorry, I'm unable to use the exexute_script function due to the website having a bot detector – Coffee Guy Jan 19 '21 at 11:47

2 Answers2

1
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

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • one min, lemme try – Coffee Guy Jan 19 '21 at 12:00
  • Nope, Sorry, The location is still based on the browser window, rather than the screen, and isn't solving the problem – Coffee Guy Jan 19 '21 at 12:06
  • @CoffeeGuy what you mean by browser window ? means it showing only element within the view port ? – PDHide Jan 19 '21 at 12:09
  • What I want is that the coordinates of the element, are according to the browser window, For eg. if i have something right at the top of the browser tab, the element location is being shown according to the browser window (0y as it is at the top) rather than the actual location, which should be shown according to the screen – Coffee Guy Jan 19 '21 at 12:11
  • @CoffeeGuy why would you want that selenium don't have access to screen outside browser – PDHide Jan 19 '21 at 12:14
  • 1
    wdym? I didnt say that selenium shouldnt have access to screen outside browser, One minute, lemme take a screenshot and edit it with paint, to visually represent what im trying to say – Coffee Guy Jan 19 '21 at 12:16
  • @CoffeeGuy driver.get_window_position() did you try this ? get the postion and x and y of elem.location to get final – PDHide Jan 19 '21 at 12:30
  • That gives the window location, not the elements location, which I want – Coffee Guy Jan 19 '21 at 12:31
1

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)
Coffee Guy
  • 145
  • 1
  • 1
  • 8
The Pilot Dude
  • 2,091
  • 2
  • 6
  • 24