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


Posted in Python onOctober 14, 2018

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]) #切换到最新窗口的句柄

以上这篇Python+selenium 获取浏览器窗口坐标、句柄的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
跟老齐学Python之变量和参数
Oct 10 Python
Python中装饰器兼容加括号和不加括号的写法详解
Jul 05 Python
Python提取频域特征知识点浅析
Mar 04 Python
利用PyCharm Profile分析异步爬虫效率详解
May 08 Python
python发送多人邮件没有展示收件人问题的解决方法
Jun 21 Python
详解python中*号的用法
Oct 21 Python
PyCharm第一次安装及使用教程
Jan 08 Python
python 计算概率密度、累计分布、逆函数的例子
Feb 25 Python
python实现快递价格查询系统
Mar 03 Python
python实现计算图形面积
Feb 22 Python
Python机器学习之PCA降维算法详解
May 19 Python
解决Pytorch半精度浮点型网络训练的问题
May 24 Python
python读取文本中的坐标方法
Oct 14 #Python
Python 实现Windows开机运行某软件的方法
Oct 14 #Python
对python实时得到鼠标位置的示例讲解
Oct 14 #Python
python得到windows自启动列表的方法
Oct 14 #Python
python中协程实现TCP连接的实例分析
Oct 14 #Python
解决python "No module named pip" 的问题
Oct 13 #Python
pycharm运行出现ImportError:No module named的解决方法
Oct 13 #Python
You might like
PHP与MYSQL中UTF8编码的中文排序实例
2014/10/21 PHP
php对二维数组进行相关操作(排序、转换、去空白等)
2015/11/04 PHP
PHP vsprintf()函数格式化字符串操作原理解析
2020/07/14 PHP
选择TreeView控件的树状数据节点的JS方法(jquery)
2010/02/06 Javascript
jquery CSS选择器笔记
2010/03/29 Javascript
Javascript模块化编程(一)模块的写法最佳实践
2013/01/17 Javascript
js跑马灯代码(自写)
2013/04/17 Javascript
探讨js中的双感叹号判断
2013/11/11 Javascript
javascript中数组的sort()方法的使用介绍
2013/12/18 Javascript
jquery获取选中的文本和值的方法
2014/07/08 Javascript
js确认删除对话框适用于a标签及submit
2014/07/10 Javascript
javascript实现的上下无缝滚动效果
2016/09/19 Javascript
使用jQuery监听扫码枪输入并禁止手动输入的实现方法(推荐)
2017/03/21 jQuery
element 结合vue 在表单验证时有值却提示错误的解决办法
2018/01/22 Javascript
AngularJS中重新加载当前路由页面的方法
2018/03/09 Javascript
微信小程序实现手指触摸画板
2018/07/09 Javascript
深入理解Vue 组件之间传值
2018/08/16 Javascript
vue中子组件传递数据给父组件的讲解
2019/01/27 Javascript
jQuery内容过滤选择器与子元素过滤选择器用法实例分析
2019/02/20 jQuery
基于openlayers实现角度测量功能
2020/09/28 Javascript
python 获取本机ip地址的两个方法
2013/02/25 Python
python使用Tesseract库识别验证
2018/03/21 Python
详解python3中tkinter知识点
2018/06/21 Python
python 不以科学计数法输出的方法
2018/07/16 Python
Python中的函数式编程:不可变的数据结构
2018/10/08 Python
Python3 requests文件下载 期间显示文件信息和下载进度代码实例
2019/08/16 Python
python_mask_array的用法
2020/02/18 Python
Django 解决开发自定义抛出异常的问题
2020/05/21 Python
HTML5 LocalStorage 本地存储详细概括(多图)
2017/08/18 HTML / CSS
阿里健康官方海外旗舰店:阿里健康国际自营
2017/11/24 全球购物
文明和谐家庭事迹材料
2014/05/18 职场文书
党支部创先争优承诺书
2014/08/30 职场文书
2014年卫生工作总结
2014/11/27 职场文书
幼儿园园长新年寄语
2015/08/17 职场文书
MySql新手入门的基本操作汇总
2021/05/13 MySQL
SQL Server中的游标介绍
2022/05/20 SQL Server