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中集合类型(set)学习小结
Jan 28 Python
python opencv之分水岭算法示例
Feb 24 Python
python使用xlrd和xlwt读写Excel文件的实例代码
Sep 05 Python
ipython和python区别详解
Jun 26 Python
python里dict变成list实例方法
Jun 26 Python
python按键按住不放持续响应的实例代码
Jul 17 Python
Django ORM 查询管理器源码解析
Aug 05 Python
Django 响应数据response的返回源码详解
Aug 06 Python
PyCharm中Matplotlib绘图不能显示UI效果的问题解决
Mar 12 Python
django 扩展user用户字段inlines方式
Mar 30 Python
python批量修改交换机密码的示例
Sep 22 Python
Python存储读取HDF5文件代码解析
Nov 25 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实现改变图片直接打开为下载的方法
2015/04/14 PHP
PHP的APC模块实现上传进度条
2015/10/27 PHP
PHP生成图像验证码的方法小结(2种方法)
2016/07/18 PHP
老生常谈PHP位运算的用途
2017/03/12 PHP
PHP中常用的三种设计模式详解【单例模式、工厂模式、观察者模式】
2019/06/14 PHP
讨论html与javascript在浏览器中的加载顺序问题
2013/11/27 Javascript
Javascript的&&和||的另类用法
2014/07/23 Javascript
javascript限制用户只能输汉字中文的方法
2014/11/20 Javascript
在JavaScript中使用JSON数据
2016/02/15 Javascript
jQuery实现简单的手风琴效果
2020/04/17 jQuery
解决ztree搜索中多级菜单展示不全问题
2017/07/05 Javascript
vue2.0移除或更改的一些东西(移除index key)
2017/08/28 Javascript
详解js中let与var声明变量的区别
2020/04/05 Javascript
JS面向对象编程基础篇(一) 对象和构造函数实例详解
2020/03/03 Javascript
react-native 实现购物车滑动删除效果的示例代码
2021/01/15 Javascript
Python处理字符串之isspace()方法的使用
2015/05/19 Python
python操作ssh实现服务器日志下载的方法
2015/06/03 Python
python基于phantomjs实现导入图片
2016/05/13 Python
Python实现类的创建与使用方法示例
2017/07/25 Python
python format 格式化输出方法
2018/07/16 Python
用Python实现读写锁的示例代码
2018/11/05 Python
Python读写文件基础知识点
2019/06/10 Python
pycharm编写spark程序,导入pyspark包的3中实现方法
2019/08/02 Python
利用Python产生加密表和解密表的实现方法
2019/10/15 Python
Python try except finally资源回收的实现
2021/01/25 Python
中国专业的音频分享平台:喜马拉雅
2019/05/24 全球购物
端午节粽子促销活动方案
2014/02/02 职场文书
合作协议书
2014/04/23 职场文书
上班迟到检讨书300字
2014/10/18 职场文书
2016年中学植树节活动总结
2016/03/16 职场文书
MongoDB数据库的安装步骤
2021/06/18 MongoDB
教你用Python+selenium搭建自动化测试环境
2021/06/18 Python
MySQL非空约束(not null)案例讲解
2021/08/23 MySQL
Python必备技巧之字符数据操作详解
2022/03/23 Python
Python中Schedule模块使用详解 周期任务神器
2022/04/19 Python
Python自动操作神器PyAutoGUI的使用教程
2022/06/16 Python