Python+selenium 获取浏览器窗口坐标、句柄的方法
作者:Yon.Liu 发布时间:2023-03-21 16:21:52
标签:Python,selenium,句柄,窗口
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]) #切换到最新窗口的句柄
来源:https://blog.csdn.net/u010654583/article/details/79490940
0
投稿
猜你喜欢
- 视图代码lis = []#设置一个空列表用来存放发送的验证码,用来验证def yzm1(): res1 = &qu
- python加密打包程序加密方式:将py文件转为pyd格式安装easycython模块 pip install easycython使用ea
- 我们大致会在下列几种情况下用到: 一、文件操作(FileSystemObject)<%@ IMPORT
- 相关介绍Python是一种跨平台的计算机程序设计语言。是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。最初被设计用于编写自动
- 写入txt文件def text_save(filename, data):#filename为写入CSV文件的路径,data为要写入数据列表
- 1.INPUT和图片按钮对齐:<form method="post" action="
- ASP日期和时间函数我们经常会用到,本文列出了12个常用的asp日期和时间函数的语法及用法以作备忘!1.Now Now() 取
- 在做分类模型时候,需要在DataFrame中按照行获取数据以便于进行训练和测试。import pandas as pddict=[[1,2,
- 提起python做网络爬虫就不得不说到强大的组件urllib2。在python中正是使用urllib2这个组件来抓取网页的。urllib2是
- 要自动签到,最简单的是打开页面分析请求,然后我们用脚本实现请求的自动化。但是发现食行没有页面,只有 APP,这不是一个好消息,这意味着需要抓
- 作用:pygame一般用来做游戏注意:1.在使用pygame提供的功能之前,需要调用init方法2.在游戏结束前需要调用 quit 方法py
- 上次的故事是这样的前女友发来加密的"520快乐.pdf",我用python破解开之后,却发现...事情是这样的小哥哥还是
- 枚举是常用的功能,看看Python的枚举.from enum import EnumMonth = Enum('Month'
- 本文介绍了可以帮助简化 PHP 开发的10个项目,包括框架,类库,工具,代码。1.CakePHP Development Framework
- 本文更多将会介绍三思在日常中经常会用到的,或者虽然很少用到,但是感觉挺有意思的一些函数。分二类介绍,分别是: 著名函数篇-经常用到的函数 非
- 自带库一、datetimedatetime是Python处理日期和时间的标准库。1、获取当前日期和时间>>> from d
- #/usr/bin/env/python#coding=utf-8import sys,re,time,osmaxdata = 50000
- 数据增强的必要性深度学习在最近十年得以风靡得益于计算机算力的提高以及数据资源获取的难度下降。一个好的深度模型往往需要大量具有label的数据
- php二维数组排序测试数据 $arr = [
- 这篇文章主要介绍了Python3如何对urllib和urllib2进行重构,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参