当前位置:网站首页>selenium 浏览器(2)
selenium 浏览器(2)
2022-07-04 13:31:00 【不负韶华ღ】
概述
对浏览器的操作除了打开关闭浏览器、定位页面元素之外,比较重要的一个部分就是访问页面和获取浏览器的相关信息,主要包括获取浏览器的标题、URL、获取cookies、调用浏览器的警告窗口、浏览器导航以及切换窗口、标签页等。
功能
页面导航
浏览器的导航功能主要包括访问、前进、后退、刷新四个部分。这也是大多数浏览器都必须实现的功能。
1、访问URL
driver.get("https://blog.csdn.net")
2、后退
driver.back()
3、前进
driver.forward()
4、页面刷新
driver.refresh()
获取页面信息
每打开一个页面时,浏览器就会储存页面的相关信息。这主要包括浏览器页面的标题和URL。
1、获取标题
driver.title
2、获取URL
driver.current_url
Cookie
cookie,是某些网站为了辨别用户身份,进行Session跟踪而储存在用户本地终端上的数据(通常经过加密),由用户客户端计算机暂时或永久保存的信息。如果需要cookie来辨认用户的身份,则需要对cookie进行操作。
1、添加cookie
cookie_dict = {"name":"key", "value": "value"}
driver.add_cookie(cookie_dict)
其中,cookie的sameSite属性允许用户引导浏览器控制cookie,是否与第三方站点发起的请求一起发送。 引入其是为了防止CSRF(跨站请求伪造)攻击。当sameSite属性设置为 Strict,cookie不会与来自第三方网站的请求一起发送;当sameSite属性设置为 Lax, cookie将与第三方网站发起的GET请求一起发送。
cookie_dict = {"name":"key", "value": "value", 'sameSite': 'Strict'}
driver.add_cookie(cookie_dict)
2、获取单个cookie
driver.get_cookie("key")
3、获取所有cookie
driver.get_cookies()
4、删除单个cookie
driver.delete_cookie("key")
5、删除所有cookie
driver.delete_all_cookies()
切换Frame
1、通过element切换
# 存储网页元素
iframe = driver.find_element(By.CSS_SELECTOR, "#modal > iframe")
# 切换到选择的 iframe
driver.switch_to.frame(iframe)
2、通过name或id属性进行切换。如果 frame 或 iframe 具有 id 或 name 属性,则可以使用该属性。如果名称或 id 在页面上不是唯一的, 那么将切换到找到的第一个。
# 通过 id 切换框架
driver.switch_to.frame('buttonframe')
3、使用索引进行切换。
# 基于索引切换到第 2 个 iframe
iframe = driver.find_elements_by_tag_name('iframe')[1]
4、切换回原来的frame。
# 切回到默认内容
driver.switch_to.default_content()
切换窗口或标签页
1、创建并切换新的标签或窗口
# 打开一个新窗口并切换到新窗口
driver.switch_to.new_window("window")
# 打开新标签页并切换到新标签页
driver.switch_to.new_window("tab")
2、切换回原来的标签或窗口
original_window = driver.current_window_handle
driver.switch_to.window(original_window)
3、关闭标签或窗口
driver.close()
窗口管理
1、获取窗口大小
driver.get_window_size()
2、设置窗口大小
size = (1024, 768)
driver.set_window_size(size)
3、获取窗口位置
driver.get_window_position()
4、设置窗口位置
position = (100, 100)
driver.set_window_position(position)
5、最大最小化窗口
# 最大化
driver.maximize_window()
# 最小化
driver.minimize_window()
6、全屏模式
driver.fullscreen_window()
7、窗口截屏
driver.save_screenshot('./image.png')
8、窗口某个元素截屏
element = driver.find_element(By.CSS_SELECTOR, 'h1')
element.screenshot('./image.png')
边栏推荐
- Codeforce:c. sum of substrings
- Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding
- Five minutes per day machine learning: use gradient descent to complete the fitting of multi feature linear regression model
- es6模块化
- 都在说DevOps,你真正了解它吗?
- 如何配和弦
- Explain of SQL optimization
- leecode学习笔记-约瑟夫问题
- Techsmith Camtasia Studio 2022.0.2屏幕录制软件
- Quick introduction to automatic control principle + understanding
猜你喜欢
Leecode learning notes - Joseph problem
Ffmpeg Visual Studio development (IV): audio decoding
Ranking list of databases in July: mongodb and Oracle scores fell the most
Summary of common problems in development
如何搭建一支搞垮公司的技术团队?
LVGL 8.2 Line wrap, recoloring and scrolling
03-存储系统
(1) The standard of performance tuning and the correct posture for tuning - if you have performance problems, go to the heapdump performance community!
Explain of SQL optimization
Ultrasonic distance meter based on 51 single chip microcomputer
随机推荐
深度学习 网络正则化
Expose Ali's salary and position level
Red envelope activity design in e-commerce system
曝光一下阿里的工资待遇和职位级别
深度学习 神经网络案例(手写数字识别)
How to match chords
LVLG 8.2 circular scrolling animation of a label
es6模块化
How to build a technical team that will bring down the company?
C language personal address book management system
关于FPGA底层资源的细节问题
微博、虎牙挺进兴趣社区:同行不同路
Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding
LVGL 8.2 Menu
The performance of major mainstream programming languages is PK, and the results are unexpected
Sqlserver functions, creation and use of stored procedures
PLC模拟量输入 模拟量转换FC S_ITR (CODESYS平台)
Is BigDecimal safe to calculate the amount? Look at these five pits~~
Industrial Internet has greater development potential and more industry scenarios
LVGL 8.2 Sorting a List using up and down buttons