Search code examples
pythonpython-3.xseleniumresolutionpyautogui

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


Solution

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