python + selenium 切换窗口

from seleniumimport webdriver

from selenium.webdriver.support.uiimport WebDriverWait

import time

driver = webdriver.Chrome()#加载浏览器

driver.maximize_window()#最大化窗口

driver.implicitly_wait(10)

url ="http://wh.ganji.com/"

driver.get(url)

time.sleep(5)

driver.find_element_by_link_text("租房").click()

t = driver.title  #打印页面title

print(t)

#获取当前页的 handle , 返回的是单个

h1 = driver.current_window_handle

print(h1)

#获取所有的handle ,返回的是一个list

all = driver.window_handles

print(all)

#

#获取新页面title ,new_handle = all[-1](因为最新的在最后面)

#切换到新页面的handle driver.switch_to.windows(new_handle)

new_handle = all[-1]

driver.switch_to.window(new_handle)

t2 = driver.title

print(t2)

#关闭窗口driver.close()

driver.close()

driver.switch_to.window(h1)

t3 = driver.title

print(t3)

driver.find_element_by_link_text("整租").click()

driver.quit()

你可能感兴趣的:(python + selenium 切换窗口)