最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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ù),可以用這兩個(gè)函數(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為一個(gè)list,可使用訪問list的方法讀取句柄。

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

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

相關(guān)文章

最新評(píng)論

太仆寺旗| 汪清县| 肥东县| 涡阳县| 留坝县| 宣城市| 利津县| 临桂县| 互助| 贵阳市| 任丘市| 扎兰屯市| 汉源县| 库伦旗| 怀远县| 什邡市| 栖霞市| 新巴尔虎右旗| 正定县| 鹿邑县| 全椒县| 基隆市| 郑州市| 河津市| 林周县| 故城县| 通山县| 洪洞县| 文登市| 黄大仙区| 湛江市| 达州市| 固原市| 万荣县| 闽清县| 建瓯市| 西华县| 安徽省| 石门县| 峡江县| 阿鲁科尔沁旗|