from seleniumimport webdriver
import time
"""
多窗口切换:
current_window_handle 获得当前窗口句柄
window_handles 返回所有窗口的句柄到当前会话
switch_to.window() 切换到相应的口
"""
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("https://www.baidu.com")
# 获得百度搜索窗口句柄
search_windows = driver.current_window_handle
driver.find_element_by_link_text('登录').click()
driver.find_element_by_link_text('立即注册').click()
# 获得当前所有打开的窗口句柄
all_handle = driver.window_handles
# 进入注册窗口
for handle in all_handle:
if handle != search_windows:
driver.switch_to.window(handle)
print(driver.title)
driver.find_element_by_name("userName").send_keys('username')
driver.find_element_by_name("phone").send_keys('15387152747')
time.sleep(2)
# 关闭当前窗口
driver.close()
# 回到搜索窗口
driver.switch_to.window(search_windows)
print(driver.title)
driver.quit()
网友评论