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


猜你喜欢
- 本文实例讲述了Python实现基于TCP UDP协议的IPv4 IPv6模式客户端和服务端功能。分享给大家供大家参考,具体如下:由于目前工作
- 如下所示:device = torch.device("cuda:0" if torch.cuda.is_availab
- 请问,如何在ACCESS数据库和SQL SERVER数据库中查询?
- django {% url %} 模板标签使用inclusions/_archives.html...{% for date in date
- 共有三种推导式列表(list)推导式字典(dict)推导式集合(set)推导式列表推导式基本语法:[out_express for out_
- 1、说明PyG2Plot 原理其实非常简单,其中借鉴了 pyecharts 的实现,但是因为蚂蚁金服的 G2Plot 完全基于可视分析理论的
- 日常工作中需要对比两个Excel工作表中的数据差异是很不方便的,使用python来做就比较简单了!我们的思路是通过读取两个Excel的数据,
- 准备必须环境:Python3开始先实现一个简单的版本,直接上代码:import urllib.requestimport urllib.er
- 本文实例为大家分享了python通过实例方法名字调用方法的具体代码,供大家参考,具体内容如下案例: &nb
- 运行效果:完整代码from tkinter import *def click(num): global op op
- 前言利用Django开发网站,可以设计出非常优美的url规则,如果url的匹配规则(包含正则表达式)组织得比较好,view的结构就会比较清晰
- 测了一下django、flask、bottle、tornado 框架本身最简单的性能。对django的性能完全无语了。django、flas
- 实例076:做函数题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+.
- 简介OpenCV中使用VideoCapture类写的视频是没有音频的,如果要进一步处理音频则需要用到一个库——MoviePy,这个库是Pyt
- 方法一:在目前绝大部分数据库有分布式查询的需要。下面简单的介绍如何在oracle中配置实现跨库访问。比如现在有2个数据库服务器,安装了2个数
- 组件的基本使用注册组件注册组件就是利用Vue.component()方法,先传入一个自定义组件的名字,然后传入这个组件的配置。 Vue.co
- < % Response.CharSet="gb2312" tblna
- 转自http://rookiefly.cn/detail/69作死小能手这两天闲着没事,把自己电脑重装了,然而重装过后配置开发环境踩了一些坑
- python主要是通过thread和threading这两个模块来实现多线程支持。python的thread模块是比較底层的模块,pytho
- 1. 什么是网络爬虫简单来说,就是构建一个程序,以自动化的方式从网络上下载、解析和组织数据。就像我们浏览网页的时候,对于我们感兴趣的内容我们