ryoakg
10/31/2017 - 1:04 PM

selenium-headless-chrome.clj

(set-env! :dependencies '[[org.seleniumhq.selenium/selenium-java "2.53.1"]])

(import (org.openqa.selenium By WebDriver WebElement OutputType TakesScreenshot)
        (org.openqa.selenium.support.ui WebDriverWait ExpectedConditions)
        (org.openqa.selenium.chrome ChromeDriver ChromeOptions))

(def d (atom nil))

(->> (doto (ChromeOptions.)
       (.addArguments (into-array String ["--headless" "--disable-gpu"])))
     ChromeDriver.
     (reset! d))

(.get @d "http://example.com/")
(->> (.findElements @d (By/cssSelector "p"))
     (map #(.getText %))
     doall)

(-> @d
    (.findElement (By/cssSelector "a"))
    .click)

(.getCurrentUrl @d)
;; => "https://www.iana.org/domains/reserved"

(->> (.findElements @d (By/cssSelector "h2"))
     (map #(.getText %))
     doall)

(.quit @d)


(with-open [d (proxy [ChromeDriver java.io.Closeable]
                  [(doto (ChromeOptions.)
                     (.addArguments (into-array String ["--headless" "--disable-gpu"])))]
                  (close []
                    (.quit this)))]
  (.get d "http://example.com/")
  (->> (.findElements d (By/cssSelector "p"))
       (map #(.getText %))
       doall))
;; => ("This domain is established to be used for illustrative examples in documents. You may use this domain in examples without prior coordination or asking for permission." "More information...")