Selenium3 Hello World
今回は、Selenium3の環境を構築し、簡易動作確認を行います。ブラウザドライバの入手
Selenium3の公式ダウンロードページ(http://www.seleniumhq.org/download/よりリンクを辿り、Zipファイルをダウンロードします。
解凍したら、どこかに置きます。
サンプルソース作成
Mavenプロジェクトを作成し、pom.xmlとソースファイルを以下のように作成します。Seleniumであれば通常はJUnitで作成するのかもしれませんが、今回は、Seleniumの動作を確認したいので、普通のJavaソースで良いです。
pom.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>seleniumTest</groupId>
<artifactId>seleniumTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.5.3</version>
</dependency>
</dependencies>
</project>
seleniumTest.java
package seleniumTest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class seleniumTest {
public static void main(String[] arg) {
System.setProperty("webdriver.chrome.driver", "C:\\work\\chromedriver_win32\\chromedriver.exe");
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://www.google.com");
WebElement element = webDriver.findElement(By.name("q"));
element.sendKeys("こんにちは、世界");
element.sendKeys("\n");
}
}
webdriver.chrome.driverには先程、ダウンロードして解凍したブラウザドライバを指定します。
動作確認
Javaを実行すると、Chromeが起動され、「こんにちは、世界」で検索されます。

ページのトップへ戻る