automated testing of selenium - code processing

Because more and more trouble to log anti-climb measures, or even a 12306 invincible Figure know this was a verification code, I can only say, I choose death. This is derived from the use of selenium to get get cookies.

Because often there will be a verification code, resulting in automated testing ui we can not carry out better, how to deal with this code do?

 

 

 

 

Like the case of this slider and phone verification code, not through the normal way is no way to deal with, so generally have the following ideas to deal with this:

1, via interface, to get the corresponding information codes

2, allows developers to mess with the universal code verification code

3, injection cookies

 

So how is achieved by injecting it in the form of cookies?

First, let's open our login page by automated form, at this time we add a certain delay time, after manually log, print it out cookies information cookies information at this time we know the need.

driver = webdriver.Chrome()
driver.get("https://fly.layui.com/user/login/")
time.sleep(3)
cookies = driver.get_cookies()
print (cookies)

拿到cookies信息后,我们此时就可以去写登录的代码去免登陆了:
driver = webdriver.Chrome()
driver.get("https://fly.layui.com/user/login/")
cookies_01 = {'domain': 'fly.layui.com', 'expiry': 15522.125036, 'httpOnly': True, 'name': 'fly-layui', 'path': '/', 'secure': False, 'value': 's%3AwqpG2eBNqbfd6lmmWLc-DTiNP_.EAy1gm0u%2BCI8SMAxkFkaRWxUoGByz0g4RDXySLsjOfo'}
cookies_02 = {'domain': '.layui.com', 'expiry': 1567495108, 'httpOnly': False, 'name': 'Hm_lvt_d214947968792b839fd669a4decaaffc', 'path': '/', 'secure': False, 'value': '1551959096'}
cookies_03 = {'domain': '.layui.com', 'httpOnly': False, 'name': 'Hm_lpvt_d214947968792b839fd669a4decaaffc', 'path': '/', 'secure': False, 'value': '1558659109'}

driver.add_cookie(cookies_01)
driver.add_cookie(cookies_02)
driver.add_cookie(cookies_03)
driver.refresh()
刷新后就可以自动登录了。


但是这存在一个问题就是,如果cookies信息很多,一条条手动去添加过去麻烦,所以我们可以先把他存到一个文件中:
cookiesFile = json.dumps(cookies)
with open('cookiesFile.json', 'w') as filemy:
    filemy.write(cookiesFile)
这样你之前的cookies信息就会存在cookies文件中,下次要去使用就直接读文件就好了,具体实现如下:

with open('cookiesFile.json','r') as filemy:
    cookiesInfo=json.loads(filemy.read())

for cc in range(0,len(cookiesInfo)):

    driver.add_cookie(cookiesInfo[cc])
driver.refresh()
此时通过refresh后,我们就直接登入了系统,是不是很方便呢

Guess you like

Origin www.cnblogs.com/jiachangwei/p/12153055.html
Recommended