selenium+python 浏览器标签页跳转 switch_to_window

参考文章:http://www.cnblogs.com/paisen/p/3289708.html

 

 

 

from selenium import webdriver
import time

browser = webdriver.Chrome()
first_url = 'http://www.baidu.com'
browser.get(first_url)
browser.find_element_by_xpath('//*[@id="kw"]').send_keys('hello world')
time.sleep(3)
browser.find_element_by_xpath('//*[@id="1"]/h3/a/em').click()
browser.switch_to.window(browser.window_handles[0])
print(browser.title) # 第一个页面
browser.switch_to.window(browser.window_handles[1])
print(browser.title) # 最后一个页面
time.sleep(1)
browser.switch_to.window(browser.window_handles[0])
# browser.quit()
posted @ 2017-10-20 14:59  猪啊美  阅读(3146)  评论(0)    收藏  举报