【问题标题】:Selenium WebDriver Java code doesn't execute on eclipse: Could not find the main classSelenium WebDriver Java 代码在 Eclipse 上不执行:找不到主类
【发布时间】:2013-11-22 11:56:48
【问题描述】:

我已经安装了 jdk6 (jdk1.6.0_26) 和 jdk7 (jdk1.7.0_25)。我有以下两个 java 类:

第一个 java 文件:

package code.google.com.p.selenium;
import org.openqa.selenium.WebElement;
public class GoogleSearchPage {
    // Here's the element
    private WebElement q;

    public void searchFor(String text) {
        // And here we use it. Note that it looks like we've
        // not properly instantiated it yet....
        q.sendKeys(text);
        q.submit();
    }
}

第二个java文件:

package code.google.com.p.selenium;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.PageFactory;

public class UsingGoogleSearchPage {
    public static void main(String[] args) {
        // Create a new instance of a driver
        WebDriver driver = new HtmlUnitDriver();

        // Navigate to the right place
        driver.get("http://www.google.com/");

        // Create a new instance of the search page class
        // and initialise any WebElement fields in it.
        GoogleSearchPage page = PageFactory.initElements(driver, GoogleSearchPage.class);

        // And now do the search.
        page.searchFor("Cheese");
    }
}

我正在使用 Eclipse Indigo。在eclipse中,我做了以下步骤:

  1. 右键单击 UsingGoogleSearchPage.java
  2. 点击运行方式->Java应用程序

程序未执行。发生错误(请参见图片)。

【问题讨论】:

    标签: java eclipse selenium jvm selenium-webdriver


    【解决方案1】:

    我认为您是 Eclipse 和 Java 的新手。你需要告诉 JVM 你的主类。这意味着您需要选择包含您的 public static void main(...) 方法的类。执行将从那里开始。您可以从 eclipse 运行配置中执行此操作。步骤 -

    1) 右键点击类

    2) 运行方式 -> 运行配置...

    3) 从第一个选项卡(主选项卡)中选择包含您的主要方法的类

    4) 按应用并运行

    希望它会起作用。

    【讨论】:

    • 你能尝试只用 main 方法运行一个简单的类吗?从 main 方法只打印一些字符串。如果您的配置正确,那么它应该可以工作。
    • 我运行了简单的类:package code.google.com.p.selenium; public class TestTest { public static void main(String args[]){ System.out.println("I'm bad boy"); } }
    • 我认为一个信息很重要。我已经安装了 jdk6 (jdk1.6.0_26) 和 jdk7 (jdk1.7.0_25)
    【解决方案2】:
    1. 检查.class文件是否存在于您的输出文件夹中。要知道您的输出文件夹右键单击项目->属性->Java构建路径(检查底部)。

    2. 检查菜单中是否选中了 Project->build Automatically。

    3. 检查UsingGoogleSearchPage类是否在src文件夹中。右键Project->Properties->Java Build Path(Check source tab)。

    4. 或者删除运行配置。见here

    【讨论】:

    • 感谢合作。让我应用它
    【解决方案3】:

    我已经在我的电脑上安装了 jdk6 (jdk1.6.0_26) 和 jdk7 (jdk1.7.0_25)。

    1. 右击项目->点击属性
    2. 点击左侧面板的“Java Build Path”
    3. 在右侧面板中选择库选项卡并观察 JRE 系统库

    观察:JRE 系统库被设置为 JavaSE-1.6

    我在eclipse上将JavaSE-1.6改为JavaSE-1.7作为执行环境如下:

    1. 双击 JRE 系统库 [JavaSE-1.6]
    2. 在执行环境组合中设置值“JavaSE-1.7 (jre7)” -> 点击完成按钮
    3. 在弹出的Java Build Path中最后点击OK
    4. 现在运行 java 应用程序

    应用程序执行良好

    【讨论】:

      猜你喜欢
      • 2013-05-30
      • 2013-06-28
      • 1970-01-01
      • 2021-08-12
      • 2020-03-19
      • 2016-04-17
      • 2019-06-14
      • 2012-01-31
      • 1970-01-01
      相关资源
      最近更新 更多