2016-09-29 101 views
12

我想使用Selenium登錄到一個站點,然後將cookie導出到httpclient。無法將cookie從Selenium webdriver傳遞到clojure中的httpclient cookie存儲

(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 (clj-http.cookies/cookie-store) 
     http-cookies (map convert-selemium-cookie-to-httpclient-cookie x)] 
    (mapv (fn[x] (.addCookie cs x)) http-cookies) 
    cs)) 

(def driver (new-driver {:browser :firefox})) 
(def a (login driver ...)) ;; login 
(def c (cookies driver)) ;; get the cookies 
(def cs (add-selenium-cookies-to-httpclient-cookie-store c)) 
(println (client/get "some web site" 
        {:cookie-store cs 
         :client-params {"http.useragent" 
             "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.20 (KHTML, like Gecko) Chrome/11.0.672.2 Safari/534.20"}})) 

雖然我的webdriver成功登錄,該網站仍然存在餅乾跨會話,複製餅乾餅乾店後,client/get仍返回頁面沒有登錄,我曾嘗試在Python類似的事情,故能成功將cookie從webdriver傳輸到請求(用於python的http客戶端)。不確定java/clojure的問題在哪裏。

回答

4

您尚未指定您的依賴項的版本。我已經測試你的代碼如下:在我的回答結束

[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}