代码如下:
#conding=utf-8 """ @Author : rechin @Language: python """ from selenium import webdriver from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By driver = webdriver.Chrome('../tools/mac/chromedriver') driver.get("https://www.baidu.com") # 获取当前窗口,方便后面跳转回来 search_windows = driver.current_window_handle # 先点击登录,再在弹框中点击立即注册,打开注册页面 driver.find_element_by_link_text('登录').click() element = WebDriverWait(driver, 5, 0.5).until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, '注册'))) element.click() # driver.find_element_by_partial_link_text('注册').click() # 获取所有窗口具柄 all_handels = driver.window_handles print(all_handels.__len__()) # 进入到立即注册到窗口 for handle in all_handels: if handle != search_windows: driver.switch_to_window(handle) if(driver.title != '注册百度帐号'): print('切换注册窗口错误 ') # 进入到初始搜索到窗口 for handle in all_handels: if handle == search_windows: driver.switch_to_window(handle) if(driver.title != '百度一下,你就知道'): print('切换搜索窗口错误 ') driver.quit()
操作如下: