【问题标题】:Selenium: get coordinates or dimensions of element with PythonSelenium:使用 Python 获取元素的坐标或尺寸
【发布时间】:2013-03-08 19:11:46
【问题描述】:

我看到有一些方法可以通过 Selenium 的各种 Java 库获取元素的屏幕位置和尺寸,例如 org.openqa.selenium.Dimension,它提供 .getSize(),以及 org.openqa.selenium.PointgetLocation()

有没有办法通过 Selenium Python 绑定获取元素的位置或尺寸?

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    知道了!线索在selenium.webdriver.remote.webelement — Selenium 3.14 documentation.

    WebElements 具有属性.size.location。两者都是dict 类型。

    driver = webdriver.Firefox()
    
    e = driver.find_element_by_xpath("//someXpath")
    
    location = e.location
    size = e.size
    w, h = size['width'], size['height']
    
    print(location)
    print(size)
    print(w, h)
    

    输出:

    {'y': 202, 'x': 165}
    {'width': 77, 'height': 22}
    77 22
    

    它们还有一个名为rect 的属性,它本身就是一个dict,并包含元素的sizelocation

    【讨论】:

    • 如何在 C# 中实现这一点?我想获取一个元素的边界,然后进行一些计算并使用该元素上的 x、y 坐标单击。
    • @rahoolm 你可以做 e.click() 和 selenium 会点击它。
    • 这些坐标是什么意思?它们与屏幕上元素的位置不同,至少在我的机器上是不同的。它们与浏览器窗口大小有关吗?
    • 太棒了。正是我需要的,在我的情况下,我无法点击复选框使用 Chrome 无头模式,所以我试图弄清楚它发生了什么以及元素的实际位置是什么..
    • 如何从中提取 x 和 y 坐标(element.location())?我需要单独的 x 和 y 坐标。
    猜你喜欢
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多