【问题标题】:How to access the window size of a frame with python selenium?如何使用 python selenium 访问框架的窗口大小?
【发布时间】:2019-10-17 10:01:53
【问题描述】:

在 python selenium 中,我通过选择 iframe

frame = driver.find_element(*locator)
driver.switch_to.frame(frame)

然后我尝试访问该 iframe 的大小。我尝试了以下方法:

frame.get_window_rect()
frame.get_window_size()
frame.contentWindow.get_window_rect()
frame.contentWindow.get_window_size()

但看起来元素'frame'既没有属性'get_window_rect, nor 'get_window_size,也没有contentWindow。那么如何访问 iframe 元素/窗口/矩形/whatever 的大小呢?

在我切换到 iframe 之后我需要该信息。

【问题讨论】:

    标签: python selenium iframe


    【解决方案1】:

    get_window_rect()get_window_size() 是 webdriver 方法。 Webelements 有rectsize 属性

    frame = driver.find_element(*locator)
    rect = frame.rect # {'height': 888, 'width': 912}
    

    请注意,切换到 frame 元素后,您将无法使用它,它不再位于驱动程序上下文中,它会抛出 StaleElementReferenceException

    要在切换到 <iframe> 大小后获取它,您可以使用 <html> 标记

    rect = driver.find_element_by_tag_name('html').rect
    

    【讨论】:

    • 那么在切换到 iframe 之后如何获取 iframe 的大小?
    • @Alex 可以得到框架下的html标签大小,应该是一样的。
    • 具体怎么做呢?你能提供一个代码sn-p吗?
    • 谢谢,现在我得到了一些值,但这些是错误的值:{'x': 0.0, 'y': 0.0, 'width': 1563.0, 'height': 0.0} iframe 的高度不是 0 像素 - 更像是 8000 像素。我也尝试使用size,但在这里我的“高度”也为 0!
    • @Alex 你在框架或html标签上试过了吗?您可以在切换之前从帧中获取值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 2020-10-15
    • 2014-03-20
    • 2013-05-18
    • 1970-01-01
    • 2010-10-05
    • 2014-04-22
    相关资源
    最近更新 更多