#webdriver提供了switch_to_window()的方法,实现在不同的窗口之间切换
-
from selenium import webdriver import time driver = webdriver.Ie()
driver.implicitly_wait(10) driver.get(“http://www.baidu.com”)sreach_window = driver.current_window_handle
driver.find_element_by_link_text(“登录”).click()
driver.find_element_by_link_text(“立即注册”).click()all_handls = driver.window_handles
for handle in all_handls: if handle != sreach_window:
driver.switch_to_window(handle)
print(‘now sreach window!’)
driver.find_element_by_id(“TANGRAM__PSP_3__userName”).send_keys(“username”)
driver.find_element_by_id(“TANGRAM__PSP_3__phone”).send_keys(“13684995613”)
time.sleep(2)for handle in all_handls: if handle == sreach_window:
driver.switch_to_window(handle)
print(‘now sreach window!’)
driver.find_element_by_id(“TANGRAM__PSP_4__closeBtn”).click()
driver.find_element_by_id(“kw”).send_keys(“selenium”)
driver.find_element_by_id(“su”).click()
time.sleep(2) driver.quit()