JavaでSeleniumを動かす(Chrome Web Driver)

Java,Selenium,Web開発,システム開発

まずはじめに

自分用の( ..)φメモメモです。わかりづらかったらゴメンナサイ。今回、お仕事で大量の画面を打鍵する必要がありJava+Seleniumで自動化しちゃいました。

私の環境です(執筆時点)
Windows11(64bit)
Eclipse (Pleiades 2024.06.16)
Java8 ※ほんとは17の方が良い
selenium-chrome-drive:3.141.59 (Java17なら4.x.xが使えるが、、)
Chrome WebDriver 127
Chrome 127

インストール

準備(Chrome Web Driver導入)

  1. Stable(安定版)の chromeDriver(win64)をダウンロードする。
  2. https://googlechromelabs.github.io/chrome-for-testing/#stable
    class="aligncenter size-full wp-image-18703″ />

  3. ダウンロードしたzipファイルを解凍し、chromedriver.exeを任意のフォルダに配置します。
  4. ユーザ環境変数「webdriver.chrome.driver」を新規追加し、chromedriver.exeのパスを設定します。
  5. ユーザ環境変数「Path」を編集し、上記ディレクトリにパスを通します。

Mavenプロジェクト作成

  1. 任意のワークスペースを指定しEclipseを起動します。
  2. Mavenプロジェクトを作成します。
  3. シンプルなプロジェクトの作成にチェックを入れ、次へを押します。
  4. 任意のグループIDとアーティファクトを指定し、官僚を押します。
  5. 以下の通りPom.xmlを、ここから→ここまでを編集します
  6. 
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<groupId>Selenium</groupId>
    	<artifactId>Test</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
      
        <!--ここから-->
    	<properties>
    	    <maven.compiler.source>1.8</maven.compiler.source>
    	    <maven.compiler.target>1.8</maven.compiler.target>
    	</properties>
    	
    	<dependencies>
    	    <dependency>
    	        <groupId>org.seleniumhq.selenium</groupId>
    	        <artifactId>selenium-chrome-driver</artifactId>
    	        <version>3.141.59</version>
    	    </dependency>
    	
    	    <dependency>
    	      <groupId>junit</groupId>
    	      <artifactId>junit</artifactId>
    	      <version>4.12</version>
    	      <scope>test</scope>
    	    </dependency>
    	</dependencies>
        <!--ここまで-->
      
    </project>
    
  7. 以下の場所に、以下の「DemoSeleniumChromeWebDriver.java」を配置します。
    • ファイルの場所
    • DemoSeleniumChromeWebDriver.java の内容
    • package junit;
      
      import java.util.concurrent.TimeUnit;
      
      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 DemoSeleniumChromeWebDriver {
          private WebDriver driver;
          
          @Test
          public void tstHelloWorldGoogle() throws InterruptedException {
              driver = new ChromeDriver();
              driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
              driver.get("https://www.google.com/");
              WebElement searchBox = driver.findElement(By.name("q"));
              searchBox.sendKeys("Tech Hippo Lab");
              searchBox.submit();
              //Deliberately adding pause
              Thread.sleep(15000);
              driver.close();
          }
      }
      
  8. Pom.xmlを右クリックして「実行」>「Maven install」を選択します。
  9. 先ほど追加したJunitを右クリックして「実行」>「Junitテスト」を選択します。

以上でセレニウムが自動実行してくれるハズです。

Print Friendly, PDF & Email