A simple (features as needs arise) Clojure wrapper of Selenium Webdriver
If you want to use the truly headless/recording environment, you'll need to be running a linux host with xvfb and ffmpeg installed.
sudo apt-get install ffmpeg xvfb
sudo pacman -S ffmpeg xorg-server-xvfb
lein repl
)...Import the core namespace:
(use 'webdriver.core)
Create a driver object
Chrome:
(def driver (create-driver {:driver-type :chrome}))
(def driver (create-driver :chrome []))
(old method)Firefox:
(def driver (create-driver {:driver-type :firefox}))
(def driver (create-driver :firefox []))
(old method)Headless:
(def driver (create-driver {:driver-type :chrome :driver-args ["--headless"]}))
(def driver (create-driver :chrome ["--headless"]))
(old method)Xvfb (Linux only with xvfb installed)
(require '[webdriver.screen :as scr])
(def screen (scr/start-screen)) ;this is separate from driver so that we can have multiple drivers run in a single frame buffer
(def driver (create-driver {:driver-type :chrome :xvfb-port (:xvfb-port screen)}))
Now you can pass the driver object into the other functions in core to manipulate it.
(to driver "https://google.com")
(set-element driver :name "q" "silly memes")
(click (wait-for-element driver :xpath "//input[@value = 'Google Search'][1]"))
From functional code:
(with-webdriver [driver :driver-type :chrome :driver-args ["--headless"]]
(to driver "https://google.com")
(set-element driver :name "q" "silly memes")
(click (wait-for-element driver :xpath "//input[@value = 'Google Search'][1]")))
If you want a truly headless environment that doesn't have the issues of geckodriver --headless or chromedriver --headless:
(scr/with-screen [screen]
(with-webdriver [driver :driver-type :chrome :xvfb-port (:xvfb-port screen)]
(to driver "https://google.com")
(set-element driver :name "q" "silly memes")
(click (wait-for-element driver :xpath "//input[@value = 'Google Search'][1]"))))
If you want to record that headless session:
(scr/with-recorded-screen [screen :vid-out "/tmp/webdriver.mp4"]
(with-webdriver [driver :driver-type :chrome :xvfb-port (:xvfb-port screen)]
(to driver "https://google.com")
(set-element driver :name "q" "silly memes")
(click (wait-for-element driver :xpath "//input[@value = 'Google Search'][1]"))))
etc...
core.clj contains functions to handle common browser tasks. You can either read through that or checkout tests/webdriver/core_test.clj for unit test examples.
; in webdriver 0.10.0 this would work
(def driver (create-driver {:driver-type :chrome}))
(.getVersion (.getCapabilities driver))
(driver-quit driver)
; in webdriver 0.11.0 you'll have to do it differently
(def driver (create-driver {:driver-type :chrome}))
(.getVersion (.getCapabilities (:driver driver))) ; notice (:driver driver) instead of just driver
(driver-quit driver)
docker build --no-cache -t attackhelicopter/webdriver:0.9.0 -t attackhelicopter/webdriver:latest .
-webdriver 0.13.0
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close