Bạn chưa chỉ định phiên bản phụ thuộc của mình. Tôi đã kiểm tra mã của bạn với các thông tin sau:
[org.clojure/clojure "1.8.0"]
[clj-webdriver "0.7.2"]
[org.seleniumhq.selenium/selenium-server "3.0.0-beta4"]
Firefox 48.0.2
Mac OS Sierra 10.12
REPL phiên ở cuối câu trả lời của tôi cho thấy rằng nó hoạt động (tôi đã sử dụng https://httpbin.org/
để kiểm tra).
Để khắc tôi sẽ kiểm tra sau:
- in những gì cookie được trở về từ lái xe hoặc kiểm tra trong trình duyệt của giao diện điều khiển để xem phản hồi từ trang đăng nhập của bạn chứa tiêu đề to set cookies đúng
- in những gì bạn Tiện ích http-client cookiestore chứa
- bật ghi nhật ký gỡ lỗi cho http-client (hoặc bật ghi nhật ký gỡ lỗi trong máy chủ của bạn) để xem yêu cầu thực tế nào được gửi đến trang được bảo vệ bằng thông tin đăng nhập của bạn và nếu yêu cầu chứa cookie do trang đăng nhập đặt.
Trong phiên REPL, bạn có thể thấy cookie được trả lại từ selen chứa cookie được đặt theo yêu cầu đầu tiên. Chúng cũng có mặt trong cửa hàng cookie http-khách hàng và chúng được trả lại đúng theo URL https://httpbin.org/cookies
cho biết rằng chúng đã được gửi bởi yêu cầu của khách hàng http.
(require '[clj-webdriver.taxi :as taxi])
(require '[clj-http.client :as client])
(require '[clj-http.cookies :as cookies])
(require '[clojure.pprint :refer [pprint]])
(import java.text.SimpleDateFormat)
(import org.apache.http.impl.cookie.BasicClientCookie)
(defn convert-selemium-cookie-to-httpclient-cookie [x]
(let [sf (SimpleDateFormat. "yyyy-MM-dd")
fake-date (.parse sf "2018-08-06")]
(doto
(BasicClientCookie. (:name x) (:value x))
(.setDomain (:domain x))
(.setPath (:path x))
(.setExpiryDate (if (:expiry x) (:expiry x) fake-date))
(.setSecure (:secure? x)))))
(defn add-selenium-cookies-to-httpclient-cookie-store [x]
(let [cs (cookies/cookie-store)
http-cookies (map convert-selemium-cookie-to-httpclient-cookie x)]
(mapv (fn [x] (.addCookie cs x)) http-cookies) cs))
(def cookie-name (str "c1" (System/currentTimeMillis)))
(def cookie-value (str "v1" (System/currentTimeMillis)))
(pprint cookie-name)
;; "c11475935066134"
(pprint cookie-value)
;; "v11475935066814"
(def driver (taxi/new-driver {:browser :firefox}))
(taxi/to driver (format "https://httpbin.org/cookies/set?%s=%s" cookie-name cookie-value))
(def selenium-cookies (taxi/cookies driver))
(pprint selenium-cookies)
;; #{{:cookie
;; #object[org.openqa.selenium.Cookie 0x4dc96ce8 "c11475935066134=v11475935066814; path=/; domain=httpbin.org"],
;; :name "c11475935066134",
;; :value "v11475935066814",
;; :path "/",
;; :expiry nil,
;; :domain "httpbin.org",
;; :secure? false}}
(def http-client-cookie-store (add-selenium-cookies-to-httpclient-cookie-store selenium-cookies))
(pprint http-client-cookie-store)
;; #object[org.apache.http.impl.client.BasicCookieStore 0x6dfa86f5 "[[version: 0][name: c11475935066134][value: v11475935066814][domain: httpbin.org][path: /][expiry: Mon Aug 06 00:00:00 CEST 2018]]"]
(def http-client-response
(client/get
"https://httpbin.org/cookies"
{:cookie-store http-client-cookie-store}))
(pprint http-client-response)
;; {:status 200,
;; :headers
;; {"Server" "nginx",
;; "Date" "Sat, 08 Oct 2016 13:58:01 GMT",
;; "Content-Type" "application/json",
;; "Content-Length" "64",
;; "Connection" "close",
;; "Access-Control-Allow-Origin" "*",
;; "Access-Control-Allow-Credentials" "true"},
;; :body
;; "{\n \"cookies\": {\n \"c11475935066134\": \"v11475935066814\"\n }\n}\n",
;; :request-time 1001,
;; :trace-redirects ["https://httpbin.org/cookies"],
;; :orig-content-encoding nil}
Nguồn
2016-10-08 09:34:37
Tôi đang sử dụng clojure thay vì python. Đối với phiên bản python, tôi đã làm cho nó. –