0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Seleniumインストール手順とサンプルコードの実装【Windows】【Java】【Edge】

0
Posted at

はじめに

会社でSeleniumを使ったテスト自動化のやって、個人のPCでもやってみたくて
備忘録として残すことにしました。

この記事では以下の環境で動作確認しています。

  • OS:Windows 11
  • IDE:Eclipse
  • Java:Java 21
  • ブラウザ:Microsoft Edge

また、MavenやGradleは使用せず、Selenium関連のJARファイルを手動で管理する方法を紹介します。

Seleniumとは

Webブラウザの操作を自動化するためのオープンソースのテスト自動化ツールです。
以下のような操作を自動で実行できます。

  • ページ遷移
  • 要素のクリック
  • フォームへの入力
  • スクリーンショットの取得

Java、Python、C#、JavaScriptなど複数の言語に対応したライブラリが提供されており、Chrome・Edge・Firefoxなど主要ブラウザで動作します。

必要なもの

  • Windows11搭載のPC
  • Java(インストール済みを前提に進めます)
  • Eclipse または他のIDE
  • Selenium
  • EdgeDriver

事前準備

(1) Seleniumのダウンロード

Selenium公式サイトから言語に合わせてダウンロードします。
今回はJavaを選択。
スクリーンショット 2026-09-04 205547.png
selenium-java-4.48.0.zipがダウンロードされました!

(2) EdgeDriverのダウンロード

※使用するブラウザのバージョンを確認し、合ったドライバーを選んでください。
バージョンが違うとエラーで動きません。

Microsoft Edge WebDriverへ移動します。

スクリーンショット 2026-09-04 210921.png
私のPCのバージョンは「152.0.4191.53(64 ビット)」だったので、これに対応するドライバーをダウンロードします。
152.0.4191 ←ここと一致するものを選んでください。

edgedriver_win64.zipがダウンロードされました!

<バージョン確認方法>
適当にEdgeを開き、アドレスバーに「edge://settings/help」入力してEnter
スクリーンショット 2026-09-04 210359.png

Eclipseでの準備

(1) プロジェクトの準備

Eclipseを起動して新プロジェクトを作成します。

( selenium-test にしてみました)

スクリーンショット 2026-09-05 181133.png

(2) ダウンロード資材を配置

(2)-1 SeleniumのJARを配置

Seleniumの依存ライブラリを置くためのフォルダ(lib)を作成します。
スクリーンショット 2026-09-05 182204.png

先ほどダウンロードしたSeleniumのzipフォルダを解凍し、中身をすべてlibフォルダにコピーします。
スクリーンショット 2026-09-05 182253.png

(2)-2 EdgeDriverのJARを配置

EdgeDriverを置くためのフォルダを作成します。(名前は何でもよいです。私はdriversにしてみました。)
先ほどダウンロードしたzipフォルダを解凍し、「msedgedriver.exe」をフォルダにコピーします。
スクリーンショット 2026-09-05 182747.png

フォルダ構成が以下のようになっていれば配置完了です
スクリーンショット 2026-09-05 182844.png

(3) ビルドパスを通す

プロジェクト名(selenium-test)のところで右クリック
「ビルド・パス」を選択
「ビルドパスの構成」を選択
「ライブラリー」を選択
「JARの追加」を選択
libに入れた全てのjarを追加し、「適用して閉じる」を選択
スクリーンショット 2026-09-05 194646.png

Seleniumを実際に動かしてみよう

src配下に新しいJavaClassを作成します.(名前はSampleTestにしました。)

サンプルコードを書いてみます。

SampleTest.java
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;

public class SampleTest {

	public static void main(String[] args)throws Exception {
		// Edgeドライバーの設定
		System.setProperty("webdriver.edge.driver", "drivers/msedgedriver.exe");
		
		// WebDriverの起動
		WebDriver driver = new EdgeDriver();
		
		// ウィンドウズ最大化
		driver.manage().window().maximize();
				
		// googleの検索ページに遷移
		driver.get("https://www.google.com/");
		
		// 検索フォームに「Selenium Java」と入力して、Enter
		WebElement searchBox = driver.findElement(By.name("q"));
		searchBox.sendKeys("Selenium Java");
		searchBox.sendKeys(Keys.ENTER);
				
		// 検索結果のページタイトルを取得
		String pageTitle = driver.getTitle();
		System.out.println(pageTitle);
		
		// スクリーンショットを取得
		try {
		    File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
		    File destFile = new File("/selenium-test/screenshots/search_result.png");
		    
		    // 保存先フォルダが存在しない場合は作成
		    destFile.getParentFile().mkdirs();
		    
		    Files.copy(srcFile.toPath(), destFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
		    System.out.println("スクリーンショットを保存しました: " + destFile.getAbsolutePath());
		} catch (IOException e) {
		    e.printStackTrace();
		}
		
		
		// ドライバを閉じる
		driver.quit();

	}

}

実装出来たら、実行します。
Edgeが開き、検索と検索結果のスクリーンショットが保存されれば成功です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?