Yi's Blog


Minimum Selenium Tutorial

Selenium is an umbrella project for a range of tools and libraries that enable and support the automation of web browsers. In my own words, it’s a solution for you to manipulate a browser in scripts.

As you can imagine, this can be useful in many ways. In tech companies, this is used to test web pages. For personal projects, we can use it to crawl some information which is hard to query directly.

There are quite a few programming languages supported to write Selenium scripts. Here in this tutorial, I will use Java. As a minimum Selenium tutorial, CodeRunner, which means the native macOS application, not the VS Code extension, will be used instead of a completed IDE.

First, download ChromeDriver from this Google site, which is a binary file, and save it somewhere for future usage. Please pay attention to the version you are downloading, as you will need the exact version as your Chrome assuming that you have already had Chrome installed.

Secondly, download Java Selenium Client from Selenium’s website.


After uncompress, it should looks like this:


The last step, open up a new CodeRunner tab with Java as the selected language. Copy and paste the following code:

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;

class Untitled {
  public static void main(String[] args)  throws InterruptedException {
    System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

    WebDriver driver = new ChromeDriver();
    Thread.sleep(5000);  // Let the user actually see something!
    WebElement searchBox = driver.findElement(By.name("q"));
    Thread.sleep(5000);  // Let the user actually see something!

To compile above code with the downloaded Selenium Java client library, some setting need to changed in CodeRunner. Click Run Settings Button “Run Settings…” button in the tool bar, and put the following code to the “Run Command” section:

java -cp "${compiler%:*}:/path/to/selenium-java-3/client-combined-3.141.59.jar:/path/to/selenium-java-3/libs/*" "${compiler#*:}"

and put the following line to the “Compile Flags” section:

 -cp "/path/to/selenium-java-3/client-combined-3.141.59.jar"

Now the setting should looks like this:


Click “Run”, and you should have Chrome running and following your script now.

- EOF -