# （十二）多窗口切换



<br>
在页面操作过程中有时候点击某个链接会弹出新的窗口，这时就需要主机切换到新打开的窗口上进行操作。WebDriver提供了switch_to.window()方法，可以实现在不同的窗口之间切换。
以百度首页和百度注册页为例，在两个窗口之间的切换如下图。
![](http://orru5lls3.bkt.clouddn.com/more_windows_n.png)
```python
from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.implicitly_wait(10)
driver.get("http://www.baidu.com")

# 获得百度搜索窗口句柄
sreach_windows = driver.current_window_handle

driver.find_element_by_link_text('登录').click()
driver.find_element_by_link_text("立即注册").click()

# 获得当前所有打开的窗口的句柄
all_handles = driver.window_handles

# 进入注册窗口
for handle in all_handles:
    if handle != sreach_windows:
        driver.switch_to.window(handle)
        print('now register window!')
        driver.find_element_by_name("account").send_keys('username')
        driver.find_element_by_name('password').send_keys('password')
        time.sleep(2)
        # ……


driver.quit()
```
在本例中所涉及的新方法如下：

* current_window_handle：获得当前窗口句柄。
* window_handles：返回所有窗口的句柄到当前会话。
* switch_to.window()：用于切换到相应的窗口，与上一节的switch_to.frame()类似，前者用于不同窗口的切换，后者用于不同表单之间的切换。




原始封面

![课程图片](https://images.unsplash.com/photo-1434873740857-1bc5653afda8?w=300)


