如何获取硒中元素的屏幕坐标? [Python]

时间:2021-01-19 11:36:39

标签: python python-3.x selenium resolution pyautogui

我想知道如何根据屏幕分辨率而不是浏览器窗口大小来获取元素的坐标,我已经尝试过这个(代码块),但它根据浏览器窗口而不是提供坐标屏幕

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

我可以使用任何替代品吗?

一个糟糕的尝试来解释我的意思:

注意:driver.execute_script 是不允许的,因为该网站有机器人拦截器 :( This is the visual representation of what im trying to say

2 个答案:

答案 0 :(得分:1)

print(element.location_once_scrolled_into_view)

尝试一下,如果这有帮助,可以在以下位置找到更多可用的方法,例如 size rect 等:

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

答案 1 :(得分:1)

您可以使用 .size.location 来获取尺寸。

试试这个:

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)
相关问题