Python获取浏览器窗口句柄过程解析
作者:小小小光子 时间:2022-05-25 10:37:33
句柄(handle)是C++程序设计中经常提及的一个术语。它并不是一种具体的、固定不变的数据类型或实体,而是代表了程序设计中的一个广义的概念。句柄一般是指获取另一个对象的方法——一个广义的指针,它的具体形式可能是一个整数、一个对象或就是一个真实的指针,而它的目的就是建立起与被访问对象之间的惟一的联系
使用 selenium 获取窗口句柄
from selenium import webdriver
import time
# chromedriver的绝对路径
# driver_path = r'D:\0SW\最临时软件\chromedriver.exe'
driver_path = r'D:\0SW\最临时软件\geckodriver.exe'
# 初始化一个driver,并且指定chromedriver的路径
# driver = webdriver.Chrome(executable_path=driver_path)
driver = webdriver.Firefox(executable_path=driver_path)
# 请求网页
driver.get("http://www.gsxt.gov.cn/index.html")
driver.execute_script("window.open('http://www.hao6v.com')")
# 打印窗口句柄
a = driver.window_handles
print(a)
# 切换窗口
driver.switch_to.window(driver.window_handles[1])
print(driver.current_url)
print(driver.page_source)
driver.switch_to.window(a[0]) # 选择第一个标签页 睡两秒 选择第二个标签页
time.sleep(2)
driver.switch_to.window(a[1])
虽然在窗口中切换到了新的页面,但是driver中还没切换
如果想要在代码中切换到新的页面,并爬取页面,那么应该使用driver.switch_to.window来切换到指定的窗口
从driver.window_handles中取出具体是 第几个窗口
driver.window_handles是一个列表,里边存储的是窗口句柄。他会按照打开的顺序来存储窗口句柄
来源:https://www.cnblogs.com/lab-zj/p/13359949.html
标签:python,浏览器,句柄
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python中print()函数的“,”与java中System.out.print()函数中的“+”功能详解
2021-12-18 10:27:02
安装MySQL的步骤和方法
2009-07-30 08:38:00
![](https://img.aspxhome.com/file/UploadPic/20097/30/20097309429851s.gif)
python状态机transitions库详解
2021-07-14 15:52:16
BootStrap创建响应式导航条实例代码
2023-08-09 02:09:55
![](https://img.aspxhome.com/file/2023/4/56054_0s.gif)
Python Django教程之实现待办事项应用程序
2022-11-13 09:39:15
![](https://img.aspxhome.com/file/2023/0/84870_0s.png)
浅谈Python批处理文件夹中的txt文件
2023-03-17 22:58:28
![](https://img.aspxhome.com/file/2023/3/65033_0s.png)
CentOS7安装Python3的教程详解
2023-10-17 02:40:28
python如何运行js语句
2022-04-10 02:34:27
![](https://img.aspxhome.com/file/2023/3/81603_0s.png)
asp如何在聊天室实现趣味答题并计分功能?
2010-06-18 20:00:00
Python 正则表达式入门(初级篇)
2021-12-03 23:41:12
为WordPress增加微博功能
2010-08-31 15:01:00
二级联动下拉菜单javascript源码
2010-03-16 12:32:00
用JavaScript判断字符串长度
2009-10-29 12:15:00
浅析Go汇编语法和MatrixOne使用介绍
2023-07-13 19:38:18
Pytorch to(device)用法
2022-05-04 22:57:19
python opencv根据颜色进行目标检测的方法示例
2021-09-29 03:53:41
![](https://img.aspxhome.com/file/2023/9/66389_0s.jpg)
python+numpy+matplotalib实现梯度下降法
2023-07-26 04:07:35
![](https://img.aspxhome.com/file/2023/8/95918_0s.gif)
win10系统中安装scrapy-1.1
2021-07-28 07:41:28
![](https://img.aspxhome.com/file/2023/8/76728_0s.png)
yii框架中的Url生产问题小结
2023-07-20 14:08:45
ASP平台如何打造金牌英文网站
2008-04-10 08:47:00