python selenium2 窗口切换实例
2017-03-16 17:03
381 查看
遍历hao123中某一区域的所有链接,点击每个链接时,会打开新的窗口,获取新窗口的title后关闭窗口,切换到初始窗口继续打开下一个链接
代码如下:
#coding=utf-8 from selenium import webdriver import time base_url = "http://www.hao123.com" driver = webdriver.Firefox() driver.get(base_url) handle_hao123 = driver.current_window_handle s = driver.find_elements_by_css_selector("ul.js_bd.site-bd.site-hd0>li>a") for i in s: i.click() all_handles = driver.window_handles #切换到新打开的窗口 for handle in all_handles: if handle != handle_hao123: driver.switch_to.window(handle) print driver.title #关闭新窗口 driver.close() #切换到初始窗口 driver.switch_to.window(handle_hao123) #为了表示最后回到初始窗口再加一个打印 print driver.title,driver.current_url driver.close()
不妥之处请指正
相关文章推荐
- python+selenium多窗口切换
- [python爬虫] Selenium切换窗口句柄及调用Chrome浏览器
- python selenium打开新窗口,多窗口切换
- python3 selenium 切换窗口的几种方法
- 基于python的selenium实现多窗口切换
- 4000 基于python的selenium实现多窗口切换
- python + selenium + chrome 如何打开新窗口,并实现窗口切换
- python3 selenium 切换窗口的几种方法小结
- Python+Selenium实现浏览器爬虫多窗口切换
- Python_selenium之窗口切换(二)
- Python_selenium之窗口切换
- 使用selenium+python;在页面A点击一个按钮跳转到页面B,页面A没有关闭,浏览器打开了一个B,切换窗口定位页面B的元素
- Python+Selenium练习篇之27-多窗口之间切换
- python selenium打开新窗口,多窗口切换
- python selenium 如何切换到链接新打开的窗口
- 一只自动化测试小白的学习记录——Python+Selenium中网页窗口切换
- selenium中窗口切换和鼠标悬停的例子
- Selenium中的多个窗口切换(3个及以上的窗口切换)
- selenium(java)浏览器多窗口切换处理