欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python+selenium 獲取瀏覽器窗口坐標(biāo)、句柄的方法

 更新時間:2018年10月14日 10:26:58   作者:Yon.Liu  
今天小編就為大家分享一篇Python+selenium 獲取瀏覽器窗口坐標(biāo)、句柄的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

1.0 獲取瀏覽器窗口坐標(biāo)

python目錄可找到Webdriver.py 文件定義了get_window_rect()函數(shù),可獲取窗口的坐標(biāo)和大?。ㄩL寬),但出現(xiàn)”Command not found”的情況。set_window_rect()函數(shù)也一樣。

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()函數(shù)和get_window_size()函數(shù),可以用這兩個函數(shù)來分別獲取窗口的坐標(biāo)和大小,而不需要用到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 #獲取當(dāng)前窗口句柄
handles = driver.window_handles #獲取所有窗口句柄

切換句柄可以使用

dr.switch_to.window(handle) #其中handle為獲取到的窗口句柄

假設(shè)handles為獲取到的所有窗口,則handles為一個list,可使用訪問list的方法讀取句柄。

dr.switch_to.windows(handles[0]) #切換到第一個窗口的句柄
dr.switch_to.windows(handles[-1]) #切換到最新窗口的句柄

以上這篇Python+selenium 獲取瀏覽器窗口坐標(biāo)、句柄的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python3.6.5基于kerberos認(rèn)證的hive和hdfs連接調(diào)用方式

    python3.6.5基于kerberos認(rèn)證的hive和hdfs連接調(diào)用方式

    這篇文章主要介紹了python3.6.5基于kerberos認(rèn)證的hive和hdfs連接調(diào)用方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • np.unique()的具體使用

    np.unique()的具體使用

    本文主要介紹了np.unique()的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Python中的Classes和Metaclasses詳解

    Python中的Classes和Metaclasses詳解

    這篇文章主要介紹了Python中的Classes和Metaclasses詳解,屬于基礎(chǔ)知識中類與對象的概念部分的深入,需要的朋友可以參考下
    2015-04-04
  • 解決Python報錯Valueerror: Expected 2d Array Got 1d Array Instead

    解決Python報錯Valueerror: Expected 2d Array 

    如您所知,每種編程語言都會遇到很多錯誤,有些是在運(yùn)行時,有些是在編譯時,下面我就來看看當(dāng)發(fā)生錯誤 ValueError: Expected 2D array, got 1D array instead時該如何解決吧
    2024-01-01
  • Python進(jìn)度條可視化之監(jiān)測程序運(yùn)行速度

    Python進(jìn)度條可視化之監(jiān)測程序運(yùn)行速度

    Tqdm是一個快速,可擴(kuò)展的Python進(jìn)度條,可以在Python長循環(huán)中添加一個進(jìn)度提示信息,用戶只需要封裝任意的迭代器即可。本文就主要介紹了通過進(jìn)度條檢測程序運(yùn)行速度,感興趣的同學(xué)可以學(xué)習(xí)一下
    2021-12-12
  • python讀csv文件時指定行為表頭或無表頭的方法

    python讀csv文件時指定行為表頭或無表頭的方法

    這篇文章主要介紹了python讀csv文件時指定行為表頭或無表頭的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • python Dijkstra算法實(shí)現(xiàn)最短路徑問題的方法

    python Dijkstra算法實(shí)現(xiàn)最短路徑問題的方法

    這篇文章主要介紹了python Dijkstra算法實(shí)現(xiàn)最短路徑問題的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Python入門教程(十九)python的函數(shù)詳解

    Python入門教程(十九)python的函數(shù)詳解

    這篇文章主要介紹了Python入門教程(十九)python的函數(shù),函數(shù)是組織好的,可重復(fù)使用的,用來實(shí)現(xiàn)單一,或相關(guān)聯(lián)功能的代碼段,需要的朋友可以參考下
    2023-04-04
  • Python獲取當(dāng)前函數(shù)名稱方法實(shí)例分享

    Python獲取當(dāng)前函數(shù)名稱方法實(shí)例分享

    這篇文章主要介紹了Python獲取當(dāng)前函數(shù)名稱方法實(shí)例分享,具有一定借鑒價值
    2018-01-01
  • Python輸入若干整數(shù)求和方式

    Python輸入若干整數(shù)求和方式

    這篇文章主要介紹了Python輸入若干整數(shù)求和方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08

最新評論