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("https://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("https://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:
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 ở đó:
Ư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:
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:
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. Syntaxc = driver.get_cookies() ck = { 'name': 'Selenium', 'value': 'Java'} driver.add_cookie(ck) ExampleCode Implementation from selenium import webdriver driver = webdriver.Chrome (executable_path="C:\chromedriver.exe") driver.maximize_window() driver.get("https://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
|