日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python获取窗口句柄_Python+selenium 获取浏览器窗口坐标、句柄的方法

發布時間:2025/3/15 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python获取窗口句柄_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]) #切換到最新窗口的句柄

以上這篇Python+selenium 獲取瀏覽器窗口坐標、句柄的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持我們。

本文標題: Python+selenium 獲取瀏覽器窗口坐標、句柄的方法

本文地址: http://www.cppcns.com/jiaoben/python/241729.html

總結

以上是生活随笔為你收集整理的python获取窗口句柄_Python+selenium 获取浏览器窗口坐标、句柄的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。