您尚未指定您的依賴項的版本。我已經測試你的代碼如下:在我的回答結束
[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會議表明,它的工作原理(我用https://httpbin.org/
進行測試)。
要解決我會檢查以下內容:看到從司機被退回或在瀏覽器控制檯檢查什麼餅乾
- 打印,如果從你的登錄頁面的響應包含頭設置正確的餅乾
- 打印你的http-client的cookiestore包含
- 爲http-client啓用調試日誌記錄(或在您的服務器上啓用調試日誌記錄)以查看發送到您的登錄保護頁面的實際請求,以及請求是否包含登錄頁面設置的cookie。
在REPL會話中,您可以看到從selenium返回的cookies包含由第一個請求設置的cookie。它們也出現在http-client cookie存儲中,並且它們被https://httpbin.org/cookies
URL正確返回,表明它們已經通過http-client與請求一起發送。
(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}
我使用clojure而不是python。對於Python版本,我已經做到了。 –