Python+selenium 获取浏览器窗口坐标、句柄的方法

作者:Yon.Liu 时间:2023-03-21 16:21:52 

1.0 获取浏览器窗口坐标

python目录可找到Webdriver.py 文件定义了get_window_rect()函数,可获取窗口的坐标和大小(长宽),但出现”Command not found”的情况。set_window_rect()函数也一样。


def get_window_rect(self):
"""
Gets the x, y coordinates of the window as well as height and width of
the current window.

:Usage:
 driver.get_window_rect()
"""
return self.execute(Command.GET_WINDOW_RECT)['value']

def set_window_rect(self, x=None, y=None, width=None, height=None):
"""
Sets the x, y coordinates of the window as well as height and width of
the current window.

:Usage:
 driver.set_window_rect(x=10, y=10)
 driver.set_window_rect(width=100, height=200)
 driver.set_window_rect(x=10, y=10, width=100, height=200)
"""
if (x is None and y is None) and (height is None and width is None):
 raise InvalidArgumentException("x and y or height and width need values")

return self.execute(Command.SET_WINDOW_RECT,
 {"x": x, "y": y, "width": width, "height": height})['value']

然而Webdriver.py文件还定义了get_window_position()函数和get_window_size()函数,可以用这两个函数来分别获取窗口的坐标和大小,而不需要用到win32gui的方法。


def get_window_size(self, windowHandle='current'):
 """
 Gets the width and height of the current window.

:Usage:
  driver.get_window_size()
 """
 command = Command.GET_WINDOW_SIZE
 if self.w3c:
  if windowHandle != 'current':
   warnings.warn("Only 'current' window is supported for W3C compatibile browsers.")
  size = self.get_window_rect()
 else:
  size = self.execute(command, {'windowHandle': windowHandle})

if size.get('value', None) is not None:
  size = size['value']

return {k: size[k] for k in ('width', 'height')}
def get_window_position(self, windowHandle='current'):
 """
 Gets the x,y position of the current window.

:Usage:
  driver.get_window_position()
 """
 if self.w3c:
  if windowHandle != 'current':
   warnings.warn("Only 'current' window is supported for W3C compatibile browsers.")
  position = self.get_window_rect()
 else:
  position = self.execute(Command.GET_WINDOW_POSITION,
        {'windowHandle': windowHandle})['value']

return {k: position[k] for k in ('x', 'y')}

2.0 获取窗口句柄


handle = driver.current_window_handle #获取当前窗口句柄
handles = driver.window_handles #获取所有窗口句柄

切换句柄可以使用


dr.switch_to.window(handle) #其中handle为获取到的窗口句柄

假设handles为获取到的所有窗口,则handles为一个list,可使用访问list的方法读取句柄。


dr.switch_to.windows(handles[0]) #切换到第一个窗口的句柄
dr.switch_to.windows(handles[-1]) #切换到最新窗口的句柄

来源:https://blog.csdn.net/u010654583/article/details/79490940

标签:Python,selenium,句柄,窗口
0
投稿

猜你喜欢

  • Python async模块使用方法杂谈

    2023-12-12 14:46:36
  • Python排序搜索基本算法之堆排序实例详解

    2021-07-10 12:35:30
  • TOPI如何使TVM代码不那么样板化

    2022-02-02 00:22:07
  • 类似google的ASP分页代码[测试通过]

    2009-03-13 12:43:00
  • Python 字节流,字符串,十六进制相互转换实例(binascii,bytes)

    2022-04-24 02:59:36
  • Python实现螺旋矩阵的填充算法示例

    2022-06-30 00:18:47
  • Python3实现的判断回文链表算法示例

    2021-04-10 05:53:25
  • Python学习之面向对象编程详解

    2023-10-12 18:48:46
  • 在Python中进行自动化单元测试的教程

    2023-07-16 04:12:30
  • Python提取特定时间段内数据的方法实例

    2023-09-12 05:21:49
  • python中的字典操作及字典函数

    2023-02-06 05:17:42
  • Django中提供的6种缓存方式详解

    2023-03-24 14:55:49
  • PHP+MYSQL实现读写分离简单实战

    2023-11-23 21:30:42
  • Django设置Postgresql的操作

    2021-10-23 09:59:56
  • CSS的未来:一些试验性CSS属性

    2011-06-10 13:20:00
  • Django框架中间件(Middleware)用法实例分析

    2021-01-04 09:27:38
  • Python使用matplotlib绘制三维参数曲线操作示例

    2021-03-30 05:55:31
  • Python之Pygame的Draw绘图

    2022-11-29 18:51:18
  • 关于Internet Explorer 8

    2009-03-22 15:40:00
  • Symfony2框架创建项目与模板设置实例详解

    2023-11-20 23:36:39
  • asp之家 网络编程 m.aspxhome.com