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 相关文章推荐
pyv8学习python和javascript变量进行交互
Dec 04 Python
python学习之第三方包安装方法(两种方法)
Jul 30 Python
python3之微信文章爬虫实例讲解
Jul 12 Python
python内置函数:lambda、map、filter简单介绍
Nov 16 Python
详解Django rest_framework实现RESTful API
May 24 Python
python3.5绘制随机漫步图
Aug 27 Python
django页面跳转问题及注意事项
Jul 18 Python
Python如何使用argparse模块处理命令行参数
Dec 11 Python
Python 使用threading+Queue实现线程池示例
Dec 21 Python
Window版下在Jupyter中编写TensorFlow的环境搭建
Apr 10 Python
python 制作本地应用搜索工具
Feb 27 Python
Python 使用 Frame tkraise() 方法在 Tkinter 应用程序中的Frame之间切换
Apr 24 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
hessian 在PHP中的使用介绍
2010/12/13 PHP
jQuery获取json后使用zy_tmpl生成下拉菜单
2015/03/27 PHP
PHP实现json_decode不转义中文的方法
2017/05/20 PHP
php 人员权限管理(RBAC)实例(推荐)
2017/05/24 PHP
PHP实现获取ip地址的5种方法,以及插入用户登录日志操作示例
2019/02/28 PHP
Jquery Ajax学习实例6 向WebService发出请求,返回DataSet(XML) 异步调用
2010/03/18 Javascript
JS判断不同分辨率调用不同的CSS样式文件实现思路及测试代码
2013/01/23 Javascript
JavaScript实现网页图片等比例缩放实现代码及调用方式
2013/02/25 Javascript
关于javascript event flow 的一个bug详解
2013/09/17 Javascript
node.js中的console.time方法使用说明
2014/12/09 Javascript
JS+CSS实现滑动切换tab菜单效果
2015/08/25 Javascript
实例讲解Jquery中隐藏hide、显示show、切换toggle的用法
2016/05/13 Javascript
js 能实现监听F5页面刷新子iframe 而父页面不刷新的方法
2016/11/09 Javascript
利用n工具轻松管理Node.js的版本
2017/04/21 Javascript
javascript 中的继承实例详解
2017/05/05 Javascript
微信小程序引用公共js里的方法的实例详解
2017/08/17 Javascript
nodejs基础之多进程实例详解
2018/12/27 NodeJs
9102了,你还不会移动端真机调试吗
2019/03/25 Javascript
Angular之jwt令牌身份验证的实现
2020/02/14 Javascript
python实现DES加密解密方法实例详解
2015/06/30 Python
Python实现信用卡系统(支持购物、转账、存取钱)
2016/06/24 Python
解决python3在anaconda下安装caffe失败的问题
2017/06/15 Python
python Crypto模块的安装与使用方法
2017/12/21 Python
pytorch中的transforms模块实例详解
2019/12/31 Python
Pyqt助手安装PyQt5帮助文档过程图解
2020/11/20 Python
Python: glob匹配文件的操作
2020/12/11 Python
Python中使用Selenium环境安装的方法步骤
2021/02/22 Python
HTML5: Web 标准最巨大的飞跃
2008/10/17 HTML / CSS
孕妇内衣和胸罩:Cake Maternity
2018/07/16 全球购物
开放系统互连参考模型
2016/06/29 面试题
do you have any Best Practice for testing
2016/06/04 面试题
行政助理的岗位职责
2014/02/18 职场文书
《世界多美呀》教学反思
2014/03/02 职场文书
安全责任书怎么写
2014/07/28 职场文书
工厂员工辞职信范文
2015/05/12 职场文书
Element实现动态表格的示例代码
2021/08/02 Javascript