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实现统计英文单词个数及字符串分割代码
May 28 Python
Python实现各种排序算法的代码示例总结
Dec 11 Python
python绘制铅球的运行轨迹代码分享
Nov 14 Python
Python标准模块--ContextManager上下文管理器的具体用法
Nov 27 Python
python Spyder界面无法打开的解决方法
Apr 27 Python
和孩子一起学习python之变量命名规则
May 27 Python
python topN 取最大的N个数或最小的N个数方法
Jun 04 Python
python多任务及返回值的处理方法
Jan 22 Python
Centos7下源码安装Python3 及shell 脚本自动安装Python3的教程
Mar 07 Python
Python requests.post方法中data与json参数区别详解
Apr 30 Python
Django视图类型总结
Feb 17 Python
python中的plt.cm.Paired用法说明
May 31 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
PHP5新特性: 更加面向对象化的PHP
2006/11/18 PHP
php用正则表达式匹配中文实例详解
2013/11/06 PHP
使用php方法curl抓取AJAX异步内容思路分析及代码分享
2014/08/25 PHP
php similar_text()函数的定义和用法
2016/05/12 PHP
jquery动态添加元素事件失效问题解决方法
2014/05/23 Javascript
JavaScript编程中容易出BUG的几点小知识
2015/01/31 Javascript
jquery控制背景音乐开关与自动播放提示音的方法
2015/02/06 Javascript
jQuery实现判断控件是否显示的方法
2017/01/11 Javascript
vue resource post请求时遇到的坑
2017/10/19 Javascript
nodejs+express搭建多人聊天室步骤
2018/02/12 NodeJs
angular中两种表单的区别(响应式和模板驱动表单)
2018/12/06 Javascript
layui动态渲染生成左侧3级菜单的方法(根据后台返回数据)
2019/09/23 Javascript
jQuery 查找元素操作实例小结
2019/10/02 jQuery
详解多线程Django程序耗尽数据库连接的问题
2018/10/08 Python
解决python3 HTMLTestRunner测试报告中文乱码的问题
2018/12/17 Python
通过python的matplotlib包将Tensorflow数据进行可视化的方法
2019/01/09 Python
python导入坐标点的具体操作
2019/05/10 Python
Python下opencv图像阈值处理的使用笔记
2019/08/04 Python
使用Python的Turtle库绘制森林的实例
2019/12/18 Python
新建文件时Pycharm中自动设置头部模板信息的方法
2020/04/17 Python
Jupyter Notebook添加代码自动补全功能的实现
2021/01/07 Python
HTML5中div、article、section的区别及使用介绍
2013/08/14 HTML / CSS
html5 css3实例教程 一款html5和css3实现的小机器人走路动画
2014/10/20 HTML / CSS
凌阳科技股份有限公司C++程序员面试题笔试题
2014/11/20 面试题
毕业生的自我鉴定
2013/10/29 职场文书
父亲追悼会答谢词
2014/01/17 职场文书
物业保安员岗位职责制度
2014/01/30 职场文书
2014年六一儿童节演讲稿
2014/05/23 职场文书
领导班子自我剖析材料
2014/08/16 职场文书
学校食堂管理制度
2015/08/04 职场文书
文明上网主题班会
2015/08/14 职场文书
2016年“我们的节日·中秋节”活动总结
2016/04/05 职场文书
党风廉政建设心得体会
2019/05/21 职场文书
如何理解Vue前后端数据交互与显示
2021/05/10 Vue.js
使用Oracle跟踪文件的问题详解
2021/06/28 Oracle
星际争霸:毕姥爷vs解冻01
2022/04/01 星际争霸