2016-09-29 13 views
12

Tôi muốn sử dụng Selenium để đăng nhập vào một trang web và sau đó xuất cookie sang httpclient.Không thể chuyển cookie từ trình quản lý web Selenium tới cửa hàng cookie httpclient trong clojure

(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"}})) 

Mặc dù tôi đăng nhập thành công trong webdriver và trang web đó vẫn tồn tại cookie xuyên suốt các phiên, sau khi sao chép các tập tin cookie cookie lưu trữ, client/get vẫn quay trở lại trang KHÔNG đăng nhập. Tôi đã thử một điều tương tự trong python, và nó có thể chuyển thành công cookie từ webdriver sang yêu cầu (http máy khách cho python). Không chắc chắn đâu là vấn đề đối với java/clojure.

Trả lời

4

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} 
Các vấn đề liên quan