如何通过命令行使用硒罐运行Java程序 [英] How to run a java program with selenium jar through command line

查看:105
本文介绍了如何通过命令行使用硒罐运行Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过命令行使用硒罐运行Java程序...

How to run a java program with selenium jar through command line...

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

public class HelloSelenium {

    public static void main(String[] args) {
        WebDriver driver;

        driver = new FirefoxDriver();
        System.out.println("Hello");
    }

}

通过CLI运行它时出现以下错误

I am getting following error on running it through CLI

java -cp.; ../jars/selenium-java-2.53.0.jar" HelloSelenium

java -cp ".;./jars/selenium-java-2.53.0.jar" HelloSelenium

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function
    at HelloSelenium.main(HelloSelenium.java:11)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
    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)
    ... 1 more

推荐答案

如果显式指定类路径,则默认情况下不包括当前目录.所以你应该做

If you specify the classpath explicitly, the current directory is not included by default. So you should do

java -cp selenium-java-2.53.0.jar;. HelloSelenium

请注意多余的;.",例如加.到类路径.

Note the extra ";.", e.g. adding . to classpath.

但是,这仍然是不够的,因为硒本身还有许多其他库作为依赖项(包含在下载的libs文件夹中).您还需要将它们添加到您的类路径中.

This will still be insufficient, however, as selenium itself has many other libraries as dependencies (included in the libs folder of your download). You will need to add those to your classpath as well.

最简单的例子是使用通配符将它们全部添加.因此,您应该使用:

Easiest to add them all with a wildcard for your simple example. So you should use:

java -cp selenium-java-2.53.0.jar;libs/*;. HelloSelenium

请注意"libs/*",它假定您位于硒下载的根文件夹中.

Note the "libs/*", which assumes you are in the root folder of your selenium download.

这篇关于如何通过命令行使用硒罐运行Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆