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中用memcached来减少数据库查询次数的教程
Apr 07 Python
Python计算三角函数之asin()方法的使用
May 15 Python
在Python中使用next()方法操作文件的教程
May 24 Python
Python面向对象编程基础解析(一)
Oct 26 Python
python实现决策树ID3算法的示例代码
May 30 Python
解析Python的缩进规则的使用
Jan 16 Python
Python实现一个带权无回置随机抽选函数的方法
Jul 24 Python
Python matplotlib绘制饼状图功能示例
Sep 10 Python
基于python操作ES实例详解
Nov 16 Python
django创建超级用户时指定添加其它字段方式
May 14 Python
pycharm进入时每次都是insert模式的解决方式
Feb 05 Python
一行代码python实现文件共享服务器
Apr 22 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脚本不报错的原因
2014/06/12 PHP
php写入、删除与复制文件的方法
2015/06/20 PHP
ext checkboxgroup 回填数据解决
2009/08/21 Javascript
jQuery EasyUI API 中文文档 可调整尺寸
2011/09/29 Javascript
jQuery插件jQuery-JSONP开发ajax调用使用注意事项
2013/11/22 Javascript
javascript判断两个IP地址是否在同一个网段的实现思路
2013/12/13 Javascript
javascript使用正则表达式检测IP地址
2014/12/03 Javascript
jQuery学习笔记之jQuery中的$
2015/01/19 Javascript
JavaScript中的函数嵌套使用
2015/06/04 Javascript
jQuery validate+artdialog+jquery form实现弹出表单思路详解
2016/04/18 Javascript
JavaScript 实现的checkbox经典实例分享
2016/10/16 Javascript
利用yarn实现一个webpack+react种子
2016/10/25 Javascript
JQuery学习总结【二】
2016/12/01 Javascript
jQuery选择器实例应用
2017/01/05 Javascript
JavaScript运动框架 链式运动到完美运动(五)
2017/05/18 Javascript
jQuery实现的页面遮罩层功能示例【测试可用】
2017/10/14 jQuery
浅谈JavaScript 声明提升
2020/09/14 Javascript
Python找出9个连续的空闲端口
2016/02/01 Python
Python 模拟登陆的两种实现方法
2017/08/10 Python
利用Django内置的认证视图实现用户密码重置功能详解
2017/11/24 Python
Python 函数返回值的示例代码
2019/03/11 Python
树莓派3 搭建 django 服务器的实例
2019/08/29 Python
Python openpyxl模块原理及用法解析
2020/01/19 Python
Python图片处理模块PIL操作方法(pillow)
2020/04/07 Python
利用CSS3动画实现圆圈由小变大向外扩散的效果实例
2018/09/10 HTML / CSS
Html5新增标签有哪些
2017/04/13 HTML / CSS
随机分配座位,共50个学生,使学号相邻的同学座位不能相邻
2014/01/18 面试题
静态变量和实例变量的区别
2015/07/07 面试题
初任培训自我鉴定
2013/10/07 职场文书
小学老师寄语大全
2014/04/04 职场文书
公司授权委托书
2014/10/17 职场文书
公务员党的群众路线教育实践活动学习心得体会
2014/10/30 职场文书
中国世界遗产导游词
2015/02/13 职场文书
SpringBoot实现quartz定时任务可视化管理功能
2021/08/30 Java/Android
Mysql binlog日志文件过大的解决
2021/10/05 MySQL
Nginx安装配置详解
2022/06/25 Servers