pyhon學習之selenium視窗切換
阿新 • • 發佈:2018-12-08
在多視窗進行操作的時候我們需要進行切換任務。看方法。
def switch_to_window(self, window_name):
""" Deprecated use driver.switch_to.window
"""
warnings.warn("use driver.switch_to.window instead",
DeprecationWarning, stacklevel=2)
self._switch_to.window(window_name)
看原始碼
def window(self, window_name): """ Switches focus to the specified window. :Args: - window_name: The name or window handle of the window to switch to. :Usage: driver.switch_to.window('main') """ if self._driver.w3c: self._w3c_window(window_name) return data = {'name': window_name} self._driver.execute(Command.SWITCH_TO_WINDOW, data)
切換到指定視窗。
window_name name或者是window的控制代碼。
使用案例。
我們怎麼獲取window的handle呢?
driver有一個 window_handles的方法
@property def window_handles(self): """ Returns the handles of all windows within the current session. :Usage: driver.window_handles """ if self.w3c: return self.execute(Command.W3C_GET_WINDOW_HANDLES)['value'] else: return self.execute(Command.GET_WINDOW_HANDLES)['value']
這裡吧window_handles宣告為一個成員,在使用的時候就按照是成員變數進行使用。
返回值就是當前session的所有的window控制代碼,但是控制代碼其實就是一個字串。
我們也可以通過driver的方法獲取當前的控制代碼 current_window_handle