当前位置:网站首页>Web automation: summary of special scenario processing methods
Web automation: summary of special scenario processing methods
2022-06-24 21:05:00 【gchh_ spring】
web Automate special scenarios :
1、 Browser reuse scenarios (chrome browser )
2、 Use cookie Complete the automatic login of the page
3、
Browser reuse scenarios (chrome browser )
Browser reuse steps :
1、 Get into chrome The installation path of the browser , And open in this path cmd Command line , Enter the following command :
chrome --remote-debugging-port=9222

This command is equivalent to opening chrome A debugging web page , Don't close pages opened in this way ,
2、 adopt python The code implements the direct reuse steps when the browser is opened 1 Debugging web page opened in , Code in definition webdriver Variable passed in options Parameters
chrome_args = webdriver.ChromeOptions()
chrome_args.debugger_address = "127.0.0.1:9222"
self.driver = webdriver.Chrome(options=chrome_args)Be careful : Before executing the code, you need to put all chrome Web page closed , Then reopen the debug page , Then run the reusable browser code ; If you have previously opened a non debug chrome Webpage , It will cause the code to get stuck
3、 If your web page needs to scan the code to log in , Then you can login in the debug browser web page first , Use steps 2 After reusing the browser , You can directly locate elements , So you can omit the login step
Use cookie Complete the automatic login of the page
cookie: It is a data cache or identity of the browser , stay cookie Without failure , The server can use the browser's cookie Data realizes the automatic login of users ,cookie The validity of is determined by the server
selenium Provides a way to get the current page directly cookie Method of information , Just take what you get cookie Data saved , Then reuse the cookie Can realize the automatic login of the web page
1、 First, log in to the web page by yourself , Through the code to get the web page cookie Information , And save to json In file
class TestLogin:
def setup_method(self):
chrome_args = webdriver.ChromeOptions()
chrome_args.debugger_address = "127.0.0.1:9222"
self.driver = webdriver.Chrome(options=chrome_args)
def test_getcookies(self):
# Use the browser to debug the web page after logging in , Get the web page directly cookie Information
cookies = self.driver.get_cookies()
# Will get cookie Information written to cookies.json In file
with open("cookies.json", "w") as f:
json.dump(cookies, f)2、 from json File read cookie Information , Realize the automatic login of web pages
def test_login(self):
# First open the page you want to log in to ( Not logged in )
self.driver.get("https://work.weixin.qq.com/")
# Read cookie Information
with open("cookies.json", "r") as f:
cookies = json.load(f)
# adopt add_cookie Methods to inject cookie,add_cookie Method to open the web page to be injected before injection , Is the first line of code
for cookie in cookies:
self.driver.add_cookie(cookie)
# After injection , Reopen the login page
self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
self.driver.find_element_by_id("menu_contacts").click()
sleep(3)
边栏推荐
- 刚购买了一个MYSQL数据库,提示已有实例,控制台登录实例要提供数据库账号,我如何知道数据库账号。
- opds sql组件能不能将流程参数通过上下文传给下一个组件
- Second understanding permutation and combination
- Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
- After idea installs these plug-ins, the code can be written to heaven. My little sister also has to arrange it
- Why do we always "give up halfway"?
- Batch capitalization of MySQL table names
- Intermediary model -- collaboration among departments
- Format method and parse method of dateformat class
- Grating diffraction
猜你喜欢

海泰前沿技术|隐私计算技术在医疗数据保护中的应用

Nifi quick installation (stand-alone / cluster)

Internet of things? Come and see Arduino on the cloud

伯克利、MIT、劍橋、DeepMind等業內大佬線上講座:邁向安全可靠可控的AI

Record a deletion bash_ Profile file
浅谈MySql update会锁定哪些范围的数据

Power apps Guide

Berkeley, MIT, Cambridge, deepmind and other industry leaders' online lectures: towards safe, reliable and controllable AI

Camera rental management system based on qt+mysql

伯克利、MIT、剑桥、DeepMind等业内大佬线上讲座:迈向安全可靠可控的AI
随机推荐
物联网?快来看 Arduino 上云啦
Sleep revolution - find the right length of rest
The Google File System (GFS) learning notes
Undo log and redo log must be clear this time
得物多活架构设计之路由服务设计
全上链哈希游戏dapp系统定制(方案设计)
"Ningwang" was sold and bought at the same time, and Hillhouse capital has cashed in billions by "selling high and absorbing low"
Material management system based on SSM (source code + document + database)
图的基本概念以及相关定义
Common data model (updating)
虚拟化是什么意思?包含哪些技术?与私有云有什么区别?
A/B测试助力游戏业务增长
伯克利、MIT、劍橋、DeepMind等業內大佬線上講座:邁向安全可靠可控的AI
Two fellow countrymen from Hunan have jointly launched a 10 billion yuan IPO
Nifi quick installation (stand-alone / cluster)
Shrimp skin test surface treated
Packaging_ Conversion between basic type and string type
Record a deletion bash_ Profile file
Otaku can't save yuan universe
Bytebase joins Alibaba cloud polardb open source database community