Selenium実践入門 Java+eclipse+Mavenでビルド後cmd実行でエラー

649 views
Skip to first unread message

石毛こずえ

unread,
Jun 23, 2021, 10:50:04 AM6/23/21
to 日本Seleniumユーザーコミュニティ
商用の自動テストツールを使用しつつ
無料の自動テストツールSeleniumについて学習している初心者です。

2016年初版と古いと思いつつ、技術評論社のSelenium実践入門を見ながら
環境を構築していきましたが、Maven+installでビルド後、コマンドプロンプトでの
実行でエラーが発生し行き詰っております。
対応方法わかりましたら、ご教授いただきたいです。

Windows10
Java1.8.0
Maven3.8.1
selenium-*-driver-3.141.59.jar
EclipseIDEVersion: 2021-03 (4.19.0)
FireFox 89.0.2 (64 ビット)



実行とエラーは以下のとおりです。---------------------------------------------------------------------------------
C:\WINDOWS\system32>java -classpath "C:\eclipse\libs\*.jar;C:\Users\user\selenium-java\workdir\webtest\target\webtest-0.0.1-SNAPSHOT.jar" webtest.SampleScript
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 7 more
--------------------------------------------------------------------------------------------------------------------------------------
ビルド時のメッセージがこちら
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/eclipse/plugins/org.eclipse.m2e.maven.runtime.slf4j.simple_1.16.0.20200610-1735/jars/slf4j-simple-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [file:/C:/eclipse/configuration/org.eclipse.osgi/8/0/.cp/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/eclipse/plugins/org.eclipse.m2e.maven.runtime.slf4j.simple_1.16.0.20200610-1735/jars/slf4j-simple-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [file:/C:/eclipse/configuration/org.eclipse.osgi/8/0/.cp/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]
[WARNING] 
[WARNING] Some problems were encountered while building the effective settings
[WARNING] Unrecognised tag: 'blocked' (position: START_TAG seen ...</url>\r\n      <blocked>... @171:16)  @ C:\apache-maven-3.8.1\conf\settings.xml, line 171, column 16
[WARNING] 
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< maven.test:legalwebtest >-----------------------
[INFO] Building legalwebtest 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ legalwebtest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ legalwebtest ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ legalwebtest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ legalwebtest ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ legalwebtest ---
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ legalwebtest ---
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ legalwebtest ---
[INFO] Installing C:\Users\user\selenium-java\workdir\legalwebtest\target\legalwebtest-0.0.1-SNAPSHOT.jar to C:\apache-maven-3.8.1\repository\maven\test\legalwebtest\0.0.1-SNAPSHOT\legalwebtest-0.0.1-SNAPSHOT.jar
[INFO] Installing C:\Users\user\selenium-java\workdir\legalwebtest\pom.xml to C:\apache-maven-3.8.1\repository\maven\test\legalwebtest\0.0.1-SNAPSHOT\legalwebtest-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.015 s
[INFO] Finished at: 2021-06-23T23:08:12+09:00
[INFO] ------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
コードはこちら
/**
 * 
 */
package webtest;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

/**
 * @author user
 *
 */
public class SampleScript {

/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
WebDriver driver = new FirefoxDriver();
driver.findElement(By.id("guestname")).sendKeys("サンプルユーザ");
driver.findElement(By.id("goto_next")).click();
driver.quit();
        //System.out.println("Hello Maven World!");

}

}
--------------------------------------------------------------------------------------------------------------
WebDriverを使用しているものをコメントあうとして、
System.out.printlnだけでビルド、実行は問題なくできました。

以上、よろしくお願い申し上げます。



Mieko Furuhashi

unread,
Jun 25, 2021, 12:57:53 PM6/25/21
to 石毛こずえ, 日本Seleniumユーザーコミュニティ
先程投稿者だけに返事をしてしまったのと、リンクがうまく貼れなかったので再度。

ビルド時のSLF4のエラーはpom.xmlに設定がないのが問題です。
次の依存関係を設定すると消えます。slf4jVersionに関しては https://mvnrepository.com/artifact/org.slf4j/slf4j-api  にバージョン情報が載ってるので最新版に置き換えてください。
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4jVersion}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4jVersion}</version>
</dependency>
Selenium 実行時のエラーもおそらくpom.xmlに問題があると思われます。依存関係が設定されてないのではと推測します。https://www.selenium.dev/documentation/ja/selenium_installation/installing_selenium_libraries

--
このメールは Google グループのグループ「日本Seleniumユーザーコミュニティ」に登録しているユーザーに送られています。
このグループから退会し、グループからのメールの配信を停止するには seleniumjp+...@googlegroups.com にメールを送信してください。
このディスカッションをウェブ上で閲覧するには https://groups.google.com/d/msgid/seleniumjp/9c4dcfe4-0e54-4ade-984d-6492579e02fbn%40googlegroups.com にアクセスしてください。


--
Mieko Furuhashi
古橋美恵子

石毛 こずえ

unread,
Jun 25, 2021, 1:07:12 PM6/25/21
to Mieko Furuhashi, 日本Seleniumユーザーコミュニティ
古橋様
ご教授いただきましてありがとうございます。
書籍ではpom.xmlを
groupId=org.seleniumhq.selenium
artifactId=selenium-java
version=最新のバージョン番号
となっていたので、そのままでした。
最新の確認方法まで教えていただいて大変助かります。

差出人: Mieko Furuhashi <moc....@okeim.com>
送信日時: 2021年6月26日 1:57
宛先: 石毛 こずえ <ish...@kailash-tech.jp>
CC: 日本Seleniumユーザーコミュニティ <selen...@googlegroups.com>
件名: Re: [selenium-jp: 1113] Selenium実践入門 Java+eclipse+Mavenでビルド後cmd実行でエラー
 
Reply all
Reply to author
Forward
0 new messages