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实现的HTTP并发测试完整示例
Apr 23 Python
Python实现的将文件每一列写入列表功能示例【测试可用】
Mar 19 Python
Python实现求解括号匹配问题的方法
Apr 17 Python
python爬取网页内容转换为PDF文件
Jul 28 Python
flask框架使用orm连接数据库的方法示例
Jul 16 Python
Python实现批量修改图片格式和大小的方法【opencv库与PIL库】
Dec 03 Python
python将txt文件读取为字典的示例
Dec 22 Python
对Python多线程读写文件加锁的实例详解
Jan 14 Python
Python登录系统界面实现详解
Jun 25 Python
python3 assert 断言的使用详解 (区别于python2)
Nov 27 Python
sklearn+python:线性回归案例
Feb 24 Python
解决Pycharm 中遇到Unresolved reference 'sklearn'的问题
Jul 13 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异步执行的详解
2013/06/03 PHP
php 模拟POST提交的2种方法详解
2013/06/17 PHP
php curl选项列表(超详细)
2013/07/01 PHP
php pdo操作数据库示例
2017/03/10 PHP
PHP数组访问常用方法解析
2020/09/05 PHP
ExtJS下 Ext.Direct加载和提交过程排错小结
2013/04/02 Javascript
导航跟随滚动条置顶移动示例代码
2013/09/11 Javascript
node.js超时timeout详解
2014/11/26 Javascript
JavaScript对数组进行随机重排的方法
2015/07/22 Javascript
javascript基本语法
2016/05/31 Javascript
微信小程序 解析网页内容详解及实例
2017/02/22 Javascript
JavaScript高阶函数_动力节点Java学院整理
2017/06/28 Javascript
基于JavaScript实现飘落星星特效
2017/08/10 Javascript
JavaScript实现多重继承的方法分析
2018/01/09 Javascript
vue中实现Monaco Editor自定义提示功能
2019/07/05 Javascript
[42:00]完美世界DOTA2联赛PWL S3 Phoenix vs INK ICE 第一场 12.13
2020/12/17 DOTA
pyqt4教程之widget使用示例分享
2014/03/07 Python
python实现爬虫下载美女图片
2015/07/14 Python
如何处理Python3.4 使用pymssql 乱码问题
2016/01/08 Python
用生成器来改写直接返回列表的函数方法
2017/05/25 Python
让你Python到很爽的加速递归函数的装饰器
2019/05/26 Python
django解决订单并发问题【推荐】
2019/07/31 Python
基于pytorch的保存和加载模型参数的方法
2019/08/17 Python
pytorch下大型数据集(大型图片)的导入方式
2020/01/08 Python
Python生成器常见问题及解决方案
2020/03/21 Python
python+pygame实现坦克大战小游戏的示例代码(可以自定义子弹速度)
2020/08/11 Python
洛佩桑酒店官方网站:Lopesan Hotels
2019/04/15 全球购物
ZWILLING双立人法国网上商店:德国刀具锅具厨具品牌
2019/08/28 全球购物
户籍证明的格式
2014/01/13 职场文书
中介公司区域经理岗位职责范本
2014/03/02 职场文书
工厂门卫岗位职责范本
2014/04/04 职场文书
中学生爱国演讲稿
2014/09/05 职场文书
2014年师德师风自我剖析材料
2014/09/27 职场文书
2015试用期转正工作总结
2014/12/12 职场文书
事业单位考察材料范文
2014/12/25 职场文书
mysql查找连续出现n次以上的数字
2022/05/11 MySQL