Skip to content

Instantly share code, notes, and snippets.

@iku000888
Created June 9, 2017 01:48
Show Gist options
  • Save iku000888/e82ca89f30da21bcbfdb5f7eafb90082 to your computer and use it in GitHub Desktop.
Save iku000888/e82ca89f30da21bcbfdb5f7eafb90082 to your computer and use it in GitHub Desktop.
Using Selenium Web driver from Clojure
(ns anpi-selenium.core
(:import org.openqa.selenium.By
org.openqa.selenium.chrome.ChromeDriver
org.openqa.selenium.htmlunit.HtmlUnitDriver))
;; Dependency: [org.seleniumhq.selenium/selenium-java "3.4.0"]
;;; Original Java Tutorial
;;; https://github.com/SeleniumHQ/selenium/wiki/Getting-Started
;;; Google search box example
(let [driver (HtmlUnitDriver.)]
(.get driver "https://www.google.com")
(let [el (.findElement driver (By/name "q"))]
(doto el
(.sendKeys (into-array ["Cheese!"]))
.submit))
(let [title (.getTitle driver)]
(.quit driver)
title))
;;; An example that requires javascript running
(let [driver (ChromeDriver.)]
(.get driver "http://www.google.com/webhp?complete=1&hl=en")
(let [query (.findElement driver (By/name "q"))]
(.sendKeys query (into-array ["Cheese"]))
;;Figure out a more efficient way to wait for the render completion
(Thread/sleep 2000)
(let [suggestions (.findElements driver (By/xpath "//div[@class='sbqs_c']"))]
(doseq [s suggestions]
(prn (.getText s)))))
(.quit driver))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment