[For beginners] Run Selenium in Java

What is Selenium

It is a tool that can operate the browser automatically. It is often used in combination with tests because it can emulate user behavior.

Things necessary

Advance preparation

What to download

Download Selenium

First, drop the necessary libraries on Selenium's Download Page.

image.png

This time it's Java, so click on Java's ** Download **. (Click to start downloading the zip.)

Unzip it after downloading.

image.png

** client-combined-3.4.0-nodeps.jar ** is the main body of Selenium, and the ones under lib / are the dependent libraries. (Everything below lib / is also required to run Selenium.)

Since lib also contains JUnit, you can use it immediately.

Download Chrome Driver

Also download the Chrome Driver from the Chrome Driver Download Page.

The latest version at the moment (2017/06/14) is 2.30, so I will drop it.

image.png

Please note that the supported Chrome version differs depending on the Chrome Driver version. (Chrome Driver 2.30 is Chrome v58-60)

image.png

Download the Chrome Driver that matches your OS.

Unzip the dropped zip and you're done downloading what you need!

Run Selenium

Project preparation

Start Eclipse and create a new project. (The name is selenium-sample.) First, let's create a folder to put dependent files etc.

image.png

Placement of dependent files

I created a folder (lib) for placing dependent libraries such as Selenium and a folder (exe) for placing ChromeDriver. Next, let's place the files in each folder.

image.png

Put everything under lib / of selenium in the same folder.

Pass the build path

Next, let's pass the build path.

image.png

Pass the build path to all the jars in lib. ** Be careful not to forget to press apply! ** **

Now that you're ready to run Selenium, let's get it working.

Run Selenium.

Create a new JavaClass in src. (The name is SampleTest.)

This time, I will use the sample code in Getting Started of Chrome Driver.

Change only the path to the Chrome Driver.

Java


import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class SampleTest {
	@Test
	public void testGoogleSearch() throws InterruptedException {
	  // Optional, if not specified, WebDriver will search your path for chromedriver.
	  System.setProperty("webdriver.chrome.driver", "./exe/chromedriver");

	  WebDriver driver = new ChromeDriver();
	  driver.get("http://www.google.com/xhtml");
	  Thread.sleep(5000);  // Let the user actually see something!
	  WebElement searchBox = driver.findElement(By.name("q"));
	  searchBox.sendKeys("ChromeDriver");
	  searchBox.submit();
	  Thread.sleep(5000);  // Let the user actually see something!
	  driver.quit();
	}
}

I think that this will work if you execute it. Please, try it.

trouble shooting

Selenium doesn't work.

If you try as above and it doesn't work, see the eclipse error message.

The driver is not executable. (Many Mac people) The cause is that ChromeDriver does not have execute permission, so please grant execute permission.

Execution file is not found (Many Windows people) Probably the path does not pass, so set the path appropriately.

Finally

If you clone this repository, you can use it immediately. (Note that the chrome driver is for Mac)

Recommended Posts

[For beginners] Run Selenium in Java
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
Java for beginners, data hiding
Java application for beginners: stream
[For beginners] Minimum sample to display RecyclerView in Java
[For beginners] Explanation of classes, instances, and statics in Java
Run Java application in Azure Batch
Java for beginners, expressions and operators 2
Settings for SSL debugging in Java
[For Java beginners] About exception handling
Classes and instances Java for beginners
Tips for handling pseudo-elements in Selenium
Run an external process in Java
[For beginners] Minimum sample to update RecyclerView with DiffUtils in Java
Key points for introducing gRPC in Java
Learn Java with "So What" [For beginners]
[Java] for Each and sorted in Lambda
[For beginners] Difference between Java and Kotlin
Read xlsx file in Java with Selenium
[For beginners] How to debug in Eclipse
For the time being, run the war file delivered in Java with Docker
For JAVA learning (2018-03-16-01)
Selenium sample (Java)
Changes in Java 11
Rock-paper-scissors in Java
Selenium x Java
2017 IDE for Java
I wrote EX25 of AtCoder Programming Guide for beginners (APG4b) in java.
Pi in Java
[Java] [For beginners] How to insert elements directly in a 2D array
Java for statement
FizzBuzz in Java
[For beginners] I tried using DBUnit in Eclipse
ChatWork4j for using the ChatWork API in Java
[For beginners] I tried using JUnit 5 in Eclipse
Solution for NetBeans 8.2 not working in Java 9 environment
Capture and save from selenium installation in Java
Kantai Collection Java # 1 Classes and Objects [For Beginners]
A collection of simple questions for Java beginners
Set pop-up display for Java language in vim.
[Introduction to Java] Basics of java arithmetic (for beginners)
Let's use Java New FileIO! (Introduction, for beginners)
Run Dataflow, Java, streaming for the time being
[Java beginner's anguish] Hard-to-test code implemented in Junit
Compare PDF output in Java for snapshot testing
Enable / disable SNI in Java for each communication
Things to watch out for in Java equals
Get along with Java containers in Cloud Run
[java] sort in list
[Java] for statement, while statement
Read JSON in Java
Make Blackjack in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
[For beginners] DI ~ The basics of DI and DI in Spring ~
[Java] Package for management