Selenium is used to bypass the login Cookies

When using selenium test back often each process needs to take the login process, so naturally more a waste of time. If you encounter login you need to enter a verification code and other circumstances, it may be the Ship Even Sank die first.

In Web applications, typically by Cookie login status corresponding verification session id. That is, as long as we carry Cookies after the login, the browser will automatically recognize us as the logged on. Because every time you start the browser Selenium is an isolated environment, you can not use local Cookies have been saved values directly, we need a driver add_cookie()added Cookies manual methods to bypass the login.

Hand grab Cookie and bypass the login

As shown, after the first log in by hand, through the development of tools - web panel, see Cookie value to any request by attempting to analyze, ECSCP_ID value of session id, we only need to add this Cookie.

Cookie storage format, as shown below, each comprising a name, value, path, secure fields such as

WebDriver related operations Cookies

  • get_cookies (self): Gets the current session of the current domain name all cookies
  • get_cookie (self, name): Gets the current session cookie values ​​specified domain name corresponding to the current
  • delete_cookie (self, name): Removes the specified cookie
  • delete_all_cookies (self): Delete all cookie
  • add_cookie(self, cookie_dict): 添加cookie

Need to add one by one when you add a Cookie, cookie_dict example:

  • driver.add_cookie({'name' : 'foo', 'value' : 'bar'})
  • driver.add_cookie({'name' : 'foo', 'value' : 'bar', 'path' : '/'})
  • driver.add_cookie({'name' : 'foo', 'value' : 'bar', 'path' : '/', 'secure':True})"

Sample code:


from selenium import webdriver
from time import sleep

driver = webdriver.Chrome()

driver.get('http://39.104.14.232/ecshop/wwwroot/admin/privilege.php?act=login')
driver.add_cookie(dict(name='ECSCP_ID',value='7406d5a6f0c5bdfbebcc29b4e41cea9731d9e9f0'))
driver.get('http://39.104.14.232/ecshop/wwwroot/admin/index.php')


sleep(3)
driver.quit()

Note: You must first open a page (same domain page) in order to set a cookie, otherwise it will be reportedUnableToSetCookieException

Requests to use with Selenium bypass the login

Hand crawled Cookie has some validity, we can log in via an interface to send, and then get into Selenium Cookies are used from the response.

Sample code:

import requests
from selenium import webdriver
from time import sleep


data = dict(username='****', password='****', act='signin')
res = requests.post('http://39.104.14.232/ecshop/wwwroot/admin/privilege.php', data=data, allow_redirects=False)

esscp_id = res.cookies.get('ECSCP_ID')

driver = webdriver.Chrome()
driver.get('http://39.104.14.232/ecshop/wwwroot/admin/privilege.php?act=login')

driver.add_cookie(dict(name='ECSCP_ID',value=esscp_id))
driver.get('http://39.104.14.232/ecshop/wwwroot/admin/index.php')


sleep(3)
driver.quit()

Similarly, if a plurality of Cookies, requires individually extracted from the request response, added one by one in the Selenium.

User name, password can get micro-channel contact lockingfree

Guess you like

Origin www.cnblogs.com/superhin/p/11481803.html