当前位置:网站首页>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')
边栏推荐
- ES6 modularization
- [differential privacy and data adaptability] differential privacy code implementation series (XIV)
- (1) The standard of performance tuning and the correct posture for tuning - if you have performance problems, go to the heapdump performance community!
- Solutions aux problèmes d'utilisation de l'au ou du povo 2 dans le riz rouge k20pro MIUI 12.5
- Pandora IOT development board learning (RT thread) - Experiment 3 button experiment (learning notes)
- C language course design questions
- Implementation of macro instruction of first-order RC low-pass filter in signal processing (easy touch screen)
- 对话龙智高级咨询顾问、Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?
- 如何配和弦
- LVGL 8.2 Menu
猜你喜欢
Practical puzzle solving | how to extract irregular ROI regions in opencv
Ffmpeg Visual Studio development (IV): audio decoding
How to match chords
numpy笔记
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
No servers available for service: xxxx
LVGL 8.2 Draw label with gradient color
Introduction to asynchronous task capability of function calculation - task trigger de duplication
Details of FPGA underlying resources
如何配和弦
随机推荐
[C language] Pointer written test questions
Red envelope activity design in e-commerce system
Sqlserver functions, creation and use of stored procedures
[local differential privacy and random response code implementation] differential privacy code implementation series (13)
Introduction to asynchronous task capability of function calculation - task trigger de duplication
Digi restarts XBee Pro S2C production. Some differences need to be noted
A collection of classic papers on convolutional neural networks (deep learning classification)
Detailed explanation of visual studio debugging methods
Ali was laid off employees, looking for a job n day, headhunters came bad news
Comment configurer un accord
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
自动控制原理快速入门+理解
Why do domestic mobile phone users choose iPhone when changing a mobile phone?
LVGL 8.2 text shadow
Digi XBee 3 rf: 4 protocols, 3 packages, 10 major functions
Codeforce:c. sum of substrings
SAIC Maxus officially released its new brand "mifa", and its flagship product mifa 9 was officially unveiled!
Explain of SQL optimization
LVGL 8.2 keyboard
Programmer turns direction