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程序设计入门(4)模块和包
Jun 16 Python
Python中利用sorted()函数排序的简单教程
Apr 27 Python
Python实现解析Bit Torrent种子文件内容的方法
Aug 29 Python
Python+Selenium+PIL+Tesseract自动识别验证码进行一键登录
Sep 20 Python
python字符串的方法与操作大全
Jan 30 Python
Python3读取Excel数据存入MySQL的方法
May 04 Python
深入浅析python 中的匿名函数
May 21 Python
Python 读写文件的操作代码
Sep 20 Python
Python通过Pillow实现图片对比
Apr 29 Python
Pandas实现一列数据分隔为两列
May 18 Python
Python创建文件夹与文件的快捷方法
Dec 08 Python
十个Python自动化常用操作,即拿即用
May 10 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安装为Apache DSO
2006/10/09 PHP
php下检测字符串是否是utf8编码的代码
2008/06/28 PHP
apache+codeigniter 通过.htcaccess做动态二级域名解析
2012/07/01 PHP
腾讯微博提示missing parameter errorcode 102 错误的解决方法
2014/12/22 PHP
php图片上传类 附调用方法
2016/05/15 PHP
Yii2框架数据库简单的增删改查语法小结
2016/08/31 PHP
JS版网站风格切换实例代码
2008/10/06 Javascript
页面中body onload 和 window.onload 冲突的问题的解决
2009/07/01 Javascript
用JQuery 实现AJAX加载XML并解析的脚本
2009/07/25 Javascript
isArray()函数(JavaScript中对象类型判断的几种方法)
2009/11/26 Javascript
JavaScript伸缩的菜单简单示例
2013/12/03 Javascript
jquery中对于批量deferred的处理方法
2014/01/22 Javascript
Js实现自定义右键行为
2015/03/26 Javascript
AngularJS路由切换实现方法分析
2017/03/17 Javascript
浅谈Angular4实现热加载开发旅程
2017/09/08 Javascript
vue中axios解决跨域问题和拦截器的使用方法
2018/03/07 Javascript
node.js实现微信开发之获取用户授权
2019/03/18 Javascript
vue 使用class创建和清除水印的示例代码
2020/12/25 Vue.js
[03:42]2018完美盛典-《加冕》
2018/12/16 DOTA
[01:02:55]CHAOS vs Mineski 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/18 DOTA
python 算法 排序实现快速排序
2012/06/05 Python
Python获取网页上图片下载地址的方法
2015/03/11 Python
python 设置文件编码格式的实现方法
2017/12/21 Python
python实现换位加密算法的示例
2018/10/14 Python
Python魔法方法 容器部方法详解
2020/01/02 Python
VS2019+python3.7+opencv4.1+tensorflow1.13配置详解
2020/04/16 Python
Keras 使用 Lambda层详解
2020/06/10 Python
Python 保存加载mat格式文件的示例代码
2020/08/04 Python
Python操作Word批量生成合同的实现示例
2020/08/28 Python
python 生成正态分布数据,并绘图和解析
2020/12/21 Python
HTML5+CSS3绘制锯齿状的矩形
2016/03/01 HTML / CSS
localStorage的过期时间设置的方法详解
2018/11/26 HTML / CSS
世界上最好的足球商店:Unisport
2019/03/02 全球购物
党支部书记先进事迹
2014/01/17 职场文书
上班迟到检讨书范文
2015/05/06 职场文书
CSS 左边固定宽右边自适应的6种方法
2022/05/15 HTML / CSS