10 examples of 'selenium get cookies python' in Python

Every line of 'selenium get cookies python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
375def test_cookies(api, session):
376 @api.route("/")
377 def cookies(req, resp):
378 resp.media = {"cookies": req.cookies}
379 resp.cookies["sent"] = "true"
380
381 r = session.get(api.url_for(cookies), cookies={"hello": "universe"})
382 assert r.json() == {"cookies": {"hello": "universe"}}
383 assert "sent" in r.cookies
384
385 r = session.get(api.url_for(cookies))
386 assert r.json() == {"cookies": {"sent": "true"}}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

246def test_secure_cookies(self):
247 def cookie_app(environ, start_response):
248 req = Request(environ)
249 status = "200 OK"
250 body = '<html><body><a href="/go/">go</a></body></html>'
251 headers = [
252 ('Content-Type', 'text/html'),
253 ('Content-Length', str(len(body))),
254 ]
255 if req.path_info != '/go/':
256 headers.extend([
257 ('Set-Cookie', 'spam=eggs; secure'),
258 ('Set-Cookie', 'foo=bar;baz; secure'),
259 ])
260 else:
261 self.assertEquals(dict(req.cookies),
262 {'spam': 'eggs', 'foo': 'bar'})
263 self.assertIn('foo=bar', environ['HTTP_COOKIE'])
264 self.assertIn('spam=eggs', environ['HTTP_COOKIE'])
265 start_response(status, headers)
266 return [to_bytes(body)]
267
268 app = webtest.TestApp(cookie_app)
269
270 self.assertFalse(app.cookies)
271 res = app.get('https://localhost/')
272 self.assertEqual(app.cookies['spam'], 'eggs')
273 self.assertEqual(app.cookies['foo'], 'bar')
274 res = res.click('go')
275 self.assertEqual(app.cookies['spam'], 'eggs')
276 self.assertEqual(app.cookies['foo'], 'bar')
157def set_cookies(self, cookies):
158 """
159 Sets new cookies from a string
160 """
161 c = SimpleCookie()
162 c.load(cookies)
163 for key, m in c.items():
164 self.s.cookies.set(key, m.value)
279def load_cookie(self, session_id):
280 sessions = json.load(open( self.sessions_file ))
281 cookie_path = sessions[str(session_id)]["session_path"]
282 url = sessions[str(session_id)]["web_url"]
283 # Setting useragent to the same one the session saved with
284 useragent = sessions[str(session_id)]["useragent"]
285 profile = FirefoxProfile()
286 profile.set_preference("general.useragent.override", useragent )
287 cookies = pickle.load(open(cookie_path, "rb"))
288 try:
289 browser = Firefox(profile)
290 except:
291 error("Couldn't open browser to view session!")
292 return
293 browser.get(url)
294 browser.delete_all_cookies()
295 browser.execute_script("window.localStorage.clear()") # clear the current localStorage
296 for cookie in cookies:
297 browser.add_cookie(cookie)
298 status(f"Session {session_id} loaded")
299 browser.refresh()
300 self.browsers.append(browser)
17def extract_cookies(self, response, request):
18 wreq = WrappedRequest(request)
19 wrsp = WrappedResponse(response)
20 return self.jar.extract_cookies(wrsp, wreq)
8def get_lagaou_cookie():
9 browser = webdriver.Chrome(executable_path="D:/mylibs/chromedriver/chromedriver.exe")
10 browser.get("https://passport.lagou.com/login/login.html")
11 browser.find_element_by_xpath("/html/body/section/div[1]/div[2]/form/div[1]/input").send_keys("18875299597")
12 browser.find_element_by_xpath("/html/body/section/div[1]/div[2]/form/div[2]/input").send_keys("5201314xf")
13 browser.find_element_by_xpath("/html/body/section/div[1]/div[2]/form/div[5]/input").click()
14 browser.get("https://www.lagou.com")
15 print(browser.get_cookies())
16 browser.close()
460def _response_cookies(response):
461 if hasattr(response, 'cookiejar'): # using splash
462 return response.cookiejar
463 else: # using ExposeCookiesMiddleware
464 return get_cookiejar(response)
48def setCookieJar(self, cj):
49 self.cj = cj
50 self.http.cj = cj
322def _load_cookies(cookies, session):
323 if session.cookies:
324 for key in session.cookies:
325 if not key in cookies:
326 cookies.load(session.cookies[key].output(None, ''))
327 if session.parent:
328 _load_cookies(cookies, session.parent)
370def set_cookie(self, cookie_string):
371 """Request to set a cookie.
372
373 Note that it is NOT necessary to call this method under ordinary
374 circumstances: cookie handling is normally entirely automatic. The
375 intended use case is rather to simulate the setting of a cookie by
376 client script in a web page (e.g. JavaScript). In that case, use of
377 this method is necessary because mechanize currently does not support
378 JavaScript, VBScript, etc.
379
380 The cookie is added in the same way as if it had arrived with the
381 current response, as a result of the current request. This means that,
382 for example, if it is not appropriate to set the cookie based on the
383 current request, no cookie will be set.
384
385 The cookie will be returned automatically with subsequent responses
386 made by the Browser instance whenever that's appropriate.
387
388 cookie_string should be a valid value of the Set-Cookie header.
389
390 For example:
391
392 browser.set_cookie(
393 "sid=abcdef; expires=Wednesday, 09-Nov-06 23:12:40 GMT")
394
395 Currently, this method does not allow for adding RFC 2986 cookies.
396 This limitation will be lifted if anybody requests it.
397
398 """
399 if self._response is None:
400 raise BrowserStateError("not viewing any document")
401 if self.request.get_type() not in ["http", "https"]:
402 raise BrowserStateError("can't set cookie for non-HTTP/HTTPS "
403 "transactions")
404 cookiejar = self._ua_handlers["_cookies"].cookiejar
405 response = self.response() # copy
406 headers = response.info()
407 headers["Set-cookie"] = cookie_string
408 cookiejar.extract_cookies(response, self.request)

Related snippets