在执行 Selenium 独立的 JAR 文件时出现问题。

huangapple go评论81阅读模式
英文:

problem in executing selenium standalone jar

问题

Hi I am beginner in Java selenium, i have problem in executing selenium standalone jar I am using
eclipse version 4.15, JDk 14.0, selenium 3.14

**error got**
 Error occurred during initialization of boot layer
java.lang.module.FindException: Unable to derive module descriptor for C:\Java-selenium\selenium-server-standalone-3.141.59.jar

    package SeleniumPractice;
    import org.openqa.selenium.chrome.ChromeDriver;//error importing org.openqa.selenium.chrome.ChromeDriver not accessible
    
    public class launchBrowser {
    
        public static void main(String[] args) {
            //launch chrome browser
            System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
            ChromeDriver driver = new ChromeDriver();//Error: cannot b resolved
        }
    
    }
英文:

Hi I am beginner in Java selenium, i have problem in executing selenium standalone jar I am using
eclipse version 4.15, JDk 14.0, selenium 3.14

error got
Error occurred during initialization of boot layer
java.lang.module.FindException: Unable to derive module descriptor for C:\Java-selenium\selenium-server-standalone-3.141.59.jar

package SeleniumPractice;
import org.openqa.selenium.chrome.ChromeDriver;//error importing org.openqa.selenium.chrome.ChromeDriver not accessible

public class launchBrowser {

	public static void main(String[] args) {
		//launch chrome browser
		System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
		ChromeDriver driver = new ChromeDriver();//Error: cannot b resolved
	}

}

答案1

得分: 1

如果您正在导入一个JAR包,那么包含.jar文件的文件夹必须包含在您项目的CLASSPATH中。在这个讨论串中,您可以找到在Eclipse中设置项目类路径的说明。

您还应该考虑使用包管理器来管理依赖,例如Maven。Eclipse对Maven提供了很好的支持,您可以在这里找到详细信息。虽然这是一个Ubuntu教程,而您可能正在使用Windows,但这里的所有重要内容都是相同的。

如果您有任何其他问题,请在此回答下发表评论,我会相应地进行编辑。


如果您想尝试使用Maven,我附上了一个示例pom.xml文件,这是用于运行Selenium测试的Maven项目的配置文件。要使用这个设置运行测试,您需要进入包含pom.xml文件的文件夹,在此文件夹中运行命令mvn clean test

在使用该文件之前,请替换或删除:

  • com.example.your.domain 作为 groupId
  • your-app-name 作为 artifactId,
  • <repositories> 部分(因为我为一些内容使用私有仓库)
  • 任何不适合您的内容 - 请注意,Java版本设置为1.8,Selenium由TestNG框架管理,由Surefire插件执行,您不需要这个设置。
<?xml version="1.0" encoding="UTF-8"?>
<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>com.example.your.domain</groupId>
    <artifactId>your-app-name</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                    <properties>
                        <property>
                            <name>reporter</name>
                            <value>listenReport.Reporter</value>
                        </property>
                    </properties>
                </configuration>
            </plugin>
        </plugins>
    </build>

<!--    <repositories>
        <repository>
            You might have to add a custom repo.
        </repository>
    </repositories>-->

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
    </dependencies>
</project>
英文:

If you are importing a JAR package, then the folder containing the .jar file has to be included in the CLASSPATH of your project. In this thread you can find instructions for setting a project classpath in Eclipse.

You should also consider using a package manager for managing dependencies, for example Maven. Eclipse provides great support for Maven, you can find details here. It is Ubuntu tutorial while you are probably using Windows, but everything important is the same here.

If you'll have any additional questions, comment this answer and I'll edit it accordingly.


In case you wanted to try Maven, I've attached an example of a pom.xml file - configuration file for Maven projects - that I use for running Selenuim tests. To run the tests using this setup, you need to go to the folder with the pom.xml file. In this folder run command mvn clean test

Before using the file, replace or remove:

  • com.example.your.domain as groupId
  • your-app-name as artifactId,
  • the &lt;repositories&gt; section (since I use a private repo for a few things)
  • anything that doesn't fit you - notice that Java version is set to 1.8 and Selenium is managed by TestNG framework, which is executed by Surefire plugin, you don't need this setup.
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project
    xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
    xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;
&gt;

    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;com.example.your.domain&lt;/groupId&gt;
    &lt;artifactId&gt;your-app-name&lt;/artifactId&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;

    &lt;properties&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
        &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
        &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
    &lt;/properties&gt;

    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.0.0-M5&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;suiteXmlFiles&gt;
                        &lt;suiteXmlFile&gt;testng.xml&lt;/suiteXmlFile&gt;
                    &lt;/suiteXmlFiles&gt;
                    &lt;properties&gt;
                        &lt;property&gt;
                            &lt;name&gt;reporter&lt;/name&gt;
                            &lt;value&gt;listenReport.Reporter&lt;/value&gt;
                        &lt;/property&gt;
                    &lt;/properties&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

&lt;!--    &lt;repositories&gt;
        &lt;repository&gt;
            You might have to add a custom repo.
        &lt;/repository&gt;
    &lt;/repositories&gt;--&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.seleniumhq.selenium&lt;/groupId&gt;
            &lt;artifactId&gt;selenium-java&lt;/artifactId&gt;
            &lt;version&gt;3.141.59&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/project&gt;

huangapple
  • 本文由 发表于 2020年7月26日 08:15:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63094857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定