当前位置:网站首页>Selenium browser (2)
Selenium browser (2)
2022-07-04 15:36:00 【Live up to your youth】
summary
In addition to opening and closing the browser 、 Locate outside the page element , The more important part is to visit the page and get the relevant information of the browser , It mainly includes getting the title of the browser 、URL、 obtain cookies、 Call the warning window of the browser 、 Browser navigation and switching windows 、 Tabs, etc .
function
Page navigation
The navigation function of the browser mainly includes accessing 、 Forward 、 back off 、 Refresh four parts . This is also the function that most browsers must realize .
1、 visit URL
driver.get("https://blog.csdn.net")
2、 back off
driver.back()
3、 Forward
driver.forward()
4、 page refresh
driver.refresh()
Get page information
Every time you open a page , The browser will store information about the page . This mainly includes the title of the browser page and URL.
1、 Get the title
driver.title
2、 obtain URL
driver.current_url
Cookie
cookie, It's some websites to identify users , Conduct Session Tracking data stored on the user's local terminal ( Usually encrypted ), Information temporarily or permanently saved by the user's client computer . if necessary cookie To identify users , You need to cookie To operate .
1、 add to cookie
cookie_dict = {"name":"key", "value": "value"}
driver.add_cookie(cookie_dict)
among ,cookie Of sameSite Property allows the user to guide the browser to control cookie, Whether to send with the request initiated by the third-party site . It was introduced to prevent CSRF( Cross-site request forgery ) attack . When sameSite Property is set to Strict,cookie Will not be sent with requests from third-party websites ; When sameSite Property is set to Lax, cookie Will be initiated with third-party websites GET Request sent together .
cookie_dict = {"name":"key", "value": "value", 'sameSite': 'Strict'}
driver.add_cookie(cookie_dict)
2、 Get a single cookie
driver.get_cookie("key")
3、 Get all cookie
driver.get_cookies()
4、 Removing a single cookie
driver.delete_cookie("key")
5、 Delete all cookie
driver.delete_all_cookies()
Switch Frame
1、 adopt element Switch
# Store web page elements
iframe = driver.find_element(By.CSS_SELECTOR, "#modal > iframe")
# Switch to the selected iframe
driver.switch_to.frame(iframe)
2、 adopt name or id Property to switch . If frame or iframe have id or name attribute , You can use this property . If the name or id It's not unique on the page , So switch to the first one you find .
# adopt id Switch frames
driver.switch_to.frame('buttonframe')
3、 Use index to switch .
# Switch to... Based on index 2 individual iframe
iframe = driver.find_elements_by_tag_name('iframe')[1]
4、 Switch back to the original frame.
# Switch back to the default
driver.switch_to.default_content()
Switch windows or tabs
1、 Create and switch new tabs or windows
# Open a new window and switch to a new window
driver.switch_to.new_window("window")
# Open a new tab and switch to a new tab
driver.switch_to.new_window("tab")
2、 Switch back to the original tab or window
original_window = driver.current_window_handle
driver.switch_to.window(original_window)
3、 Close label or window
driver.close()
Window management
1、 Get window size
driver.get_window_size()
2、 Set window size
size = (1024, 768)
driver.set_window_size(size)
3、 Get the window position
driver.get_window_position()
4、 Set the window position
position = (100, 100)
driver.set_window_position(position)
5、 Maximum and minimum windows
# Maximize
driver.maximize_window()
# To minimize the
driver.minimize_window()
6、 Full screen mode
driver.fullscreen_window()
7、 Window capture
driver.save_screenshot('./image.png')
8、 Screenshot of an element in the window
element = driver.find_element(By.CSS_SELECTOR, 'h1')
element.screenshot('./image.png')
边栏推荐
- PXE network
- Implementation of web chat room
- PXE网络
- MySQL learning notes - data type (2)
- 进制乱炖
- [book club issue 13] coding format of video files
- LeetCode 1184. 公交站间的距离 ---vector顺逆时针
- Understand the context in go language in an article
- AI做题水平已超过CS博士?
- I plan to teach myself some programming and want to work as a part-time programmer. I want to ask which programmer has a simple part-time platform list and doesn't investigate the degree of the receiv
猜你喜欢
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
MP3是如何诞生的?
华为云数据库DDS产品深度赋能
What is the future of the booming intelligent Internet of things (aiot) in recent years?
中国主要城市人均存款出炉,你达标了吗?
go-zero微服务实战系列(九、极致优化秒杀性能)
Optimization method of deep learning neural network
Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture
Intelligent customer service track: Netease Qiyu and Weier technology play different ways
Dialogue with ye Yanxiu, senior consultant of Longzhi and atlassian certification expert: where should Chinese users go when atlassian products enter the post server era?
随机推荐
Redis publier et s'abonner
LeetCode 35. Search the insertion position - vector traversal (O (logn) and O (n) - binary search)
LeetCode 58. Length of the last word
AI做题水平已超过CS博士?
在芯片高度集成的今天,绝大多数都是CMOS器件
Redis shares four cache modes
压力、焦虑还是抑郁? 正确诊断再治疗
Huawei cloud database DDS products are deeply enabled
On the implementation plan of MySQL explain
【大连理工大学】考研初试复试资料分享
这几年爆火的智能物联网(AIoT),到底前景如何?
Temperature control system based on max31865
小数,指数
MySQL学习笔记——数据类型(2)
Enter the width!
Introduction of text mining tools [easy to understand]
Redis哨兵模式实现一主二从三哨兵
c# 实现定义一套中间SQL可以跨库执行的SQL语句
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"