Hướng dẫn save cookie selenium python

Trong Automation Test sử dụng Selenium WebDỉver với Python 3, nếu bạn đã đăng nhập thì làm sao lưu được trạng thái đăng nhập khi bật lại cửa sổ trình duyệt. Chúng ta phải thao tác cookie của trình duyệt. Bài hướng dẫn sau sẽ hướng dẫn lưu [Save] và nhập [import] lại Cookie

Để lưu cookie của trình duyệt chúng ta sử dụng đoạn code sau:

import selenium.webdriver

driver = selenium.webdriver.Firefox[]
driver.get["//vinasupport.com"]
pickle.dump[ driver.get_cookies[] , open["cookies.pkl","wb"]]

Để import lại cookie chúng ta sử dụng đoạn code bên dưới

import selenium.webdriver

driver = selenium.webdriver.Firefox[]
driver.get["//vinasupport.com"]
cookies = pickle.load[open["cookies.pkl", "rb"]]
for cookie in cookies:
    driver.add_cookie[cookie]

Nguồn: vinasupport.com

Khi bạn cần cookie từ phiên này sang phiên khác, có một cách khác để làm điều đó. Sử dụng tùy chọn user-data-dir của Chrome để sử dụng các thư mục làm hồ sơ. Tôi chạy:

# You need to: from selenium.webdriver.chrome.options import Options
chrome_options = Options[]
chrome_options.add_argument["user-data-dir=selenium"] 
driver = webdriver.Chrome[chrome_options=chrome_options]
driver.get["www.google.com"]

Tại đây bạn có thể thực hiện đăng nhập để kiểm tra sự tương tác của con người. Tôi làm điều này và sau đó là cookie tôi cần bây giờ mỗi khi tôi khởi động Webdriver với thư mục đó, mọi thứ đều ở trong đó. Bạn cũng có thể cài đặt các Tiện ích mở rộng theo cách thủ công và có chúng trong mỗi phiên.

Lần thứ hai tôi chạy, tất cả các cookie đều ở đó:

# You need to: from selenium.webdriver.chrome.options import Options    
chrome_options = Options[]
chrome_options.add_argument["user-data-dir=selenium"] 
driver = webdriver.Chrome[chrome_options=chrome_options]
driver.get["www.google.com"] # Now you can see the cookies, the settings, extensions, etc., and the logins done in the previous session are present here. 

Ưu điểm là bạn có thể sử dụng nhiều thư mục với các cài đặt và cookie khác nhau, Tiện ích mở rộng mà không cần tải, dỡ cookie, cài đặt và gỡ cài đặt Tiện ích mở rộng, thay đổi cài đặt, thay đổi thông tin đăng nhập qua mã, và do đó không có cách nào để có logic của chương trình bị ngắt, Vân vân.

Ngoài ra, điều này nhanh hơn so với việc phải làm tất cả bằng mã.

81 hữu ích 5 bình luận chia sẻ

When you need cookies from session to session, there is another way to do it. Use the Chrome options user-data-dir in order to use folders as profiles. I run:

# You need to: from selenium.webdriver.chrome.options import Options
chrome_options = Options[]
chrome_options.add_argument["user-data-dir=selenium"] 
driver = webdriver.Chrome[chrome_options=chrome_options]
driver.get["www.google.com"]

Here you can do the logins that check for human interaction. I do this and then the cookies I need now every time I start the Webdriver with that folder everything is in there. You can also manually install the Extensions and have them in every session.

The second time I run, all the cookies are there:

# You need to: from selenium.webdriver.chrome.options import Options    
chrome_options = Options[]
chrome_options.add_argument["user-data-dir=selenium"] 
driver = webdriver.Chrome[chrome_options=chrome_options]
driver.get["www.google.com"] # Now you can see the cookies, the settings, extensions, etc., and the logins done in the previous session are present here. 

The advantage is you can use multiple folders with different settings and cookies, Extensions without the need to load, unload cookies, install and uninstall Extensions, change settings, change logins via code, and thus no way to have the logic of the program break, etc.

Also, this is faster than having to do it all by code.

We can save and load cookies with Selenium webdriver in Python. A cookie is an information saved by the browser about the application. A cookie is stored in a key value pair.

It is generally used to hold the credentials of users. It also stores information about the user actions on the browser within the cookie file. We can add, obtain and delete cookies of the browser.

Syntax

c = driver.get_cookies[]
ck = { 'name': 'Selenium', 'value': 'Java'}
driver.add_cookie[ck]

Example

Code Implementation

from selenium import webdriver
driver = webdriver.Chrome [executable_path="C:\chromedriver.exe"]
driver.maximize_window[]
driver.get["//www.tutorialspoint.com/index.htm"]
#get current cookies
c = driver.get_cookies[]
print[c]
#count cookies with len method
print[len[c]]
# load a new cookie
ck = { 'name': 'Selenium', 'value': 'Java'}
# save new cookie
driver.add_cookie[ck]
#get new cookies and total count after addition
ch = driver.get_cookies[]
print[ch]
print[len[ch]]

Updated on 28-Dec-2020 13:50:49

  • Related Questions & Answers
  • How to send cookies with selenium webdriver?
  • Delete Cookies On All Domains using Selenium Webdriver.
  • How to set Page Load Timeout using C# using Selenium WebDriver?
  • How to download any file and save it to the desired location using Selenium Webdriver?
  • How to know the exact time to load a page using Selenium WebDriver?
  • Clear browser Cookies with Selenium WebDriver Java bindings.
  • How to wait for iFrames to load completely in Selenium webdriver?
  • How to refresh a webpage using Python Selenium Webdriver?
  • How to set window size using phantomjs and selenium webdriver in Python?
  • How to Verify Tooltip using Selenium WebDriver?
  • How to upload files using Selenium Webdriver?
  • How to get selected option using Selenium WebDriver with Python?
  • Is there any way to load an extension in chrome browser using Selenium Webdriver?
  • How to work with cookies in Selenium with python?
  • How install Selenium Webdriver with Python?

Chủ Đề