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 07 Python
Python科学画图代码分享
Nov 29 Python
微信跳一跳游戏python脚本
Apr 01 Python
python使用pandas实现数据分割实例代码
Jan 25 Python
Python3数据库操作包pymysql的操作方法
Jul 16 Python
flask中的wtforms使用方法
Jul 21 Python
使用pip发布Python程序的方法步骤
Oct 11 Python
Python FFT合成波形的实例
Dec 04 Python
Python 读取xml数据,cv2裁剪图片实例
Mar 10 Python
使用Python实现批量ping操作方法
May 06 Python
公认8个效率最高的爬虫框架
Jul 28 Python
深入了解Python 方法之类方法 & 静态方法
Aug 17 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
给多个地址发邮件的类
2006/10/09 PHP
PHP is_dir() 判断给定文件名是否是一个目录
2010/05/10 PHP
PHP中substr()与explode()函数用法分析
2014/11/24 PHP
[原创]PHP字符串中插入子字符串方法总结
2016/05/06 PHP
Thinkphp开发--集成极光推送
2017/09/15 PHP
php实现文件上传基本验证
2020/03/04 PHP
一个高效的JavaScript压缩工具下载集合
2007/03/06 Javascript
Javascript基础教程之数据类型 (布尔型 Boolean)
2015/01/18 Javascript
JavaScript Sort 的一个错误用法示例
2015/03/20 Javascript
JQuery datepicker 用法详解
2015/12/25 Javascript
jQuery实现控制文字内容溢出用省略号(…)表示的方法
2016/02/26 Javascript
js实现当鼠标移到表格上时显示这一格全部内容的代码
2016/06/12 Javascript
JS构造函数与原型prototype的区别介绍
2016/07/04 Javascript
移动端界面的适配
2017/01/11 Javascript
BootStrap 获得轮播中的索引和当前活动的焦点对象
2017/05/11 Javascript
javascript回调函数的概念理解与用法分析
2017/05/27 Javascript
详解vue slot插槽的使用方法
2017/06/13 Javascript
使用vue-resource进行数据交互的实例
2017/09/02 Javascript
微信小程序云开发实现数据添加、查询和分页
2019/05/17 Javascript
Node.js web 应用如何封装到Docker容器中
2020/09/01 Javascript
零基础写python爬虫之抓取百度贴吧并存储到本地txt文件改进版
2014/11/06 Python
python使用BeautifulSoup分析网页信息的方法
2015/04/04 Python
Python中字典的基本知识初步介绍
2015/05/21 Python
python通过cookie模拟已登录状态的初步研究
2016/11/09 Python
python中安装模块包版本冲突问题的解决
2017/05/02 Python
django模板语法学习之include示例详解
2017/12/17 Python
Python3利用Dlib实现摄像头实时人脸检测和平铺显示示例
2019/02/21 Python
使用selenium模拟登录解决滑块验证问题的实现
2019/05/10 Python
python连接mongodb集群方法详解
2020/02/13 Python
python_matplotlib改变横坐标和纵坐标上的刻度(ticks)方式
2020/05/16 Python
德国婴儿推车和儿童安全座椅商店:BABYSHOP
2016/09/01 全球购物
国际知名军事风格休闲装品牌:Alpha Industries(阿尔法工业)
2017/05/24 全球购物
cf搞笑广告词
2014/03/14 职场文书
2015年电教工作总结
2015/05/26 职场文书
超级礼物观后感
2015/06/15 职场文书
中国文明网2015年“向国旗敬礼”活动网上签名寄语
2015/09/24 职场文书