pyhon学习之selenium窗口切换

在多窗口进行操作的时候我们需要进行切换任务。看方法。

    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

猜你喜欢

转载自blog.csdn.net/rubikchen/article/details/84592580