java - Selenium WebDriver Java 代码无法在 eclipse 上执行 : Could not find the main class

标签 java eclipse selenium jvm selenium-webdriver

我已经安装了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 应用程序”

程序没有执行。发生错误(请参见图片)。 enter image description here

最佳答案

我认为您对 Eclipse 和 Java 非常陌生。您需要告诉 JVM 您的主类。这意味着您需要选择包含 public static void main(...) 方法的类。从这里开始执行。您可以通过 eclipse 运行配置来执行此操作。步骤-

1) 右键单击​​该类

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

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

4) 按应用并运行

希望它能发挥作用。

关于java - Selenium WebDriver Java 代码无法在 eclipse 上执行 : Could not find the main class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20144358/

相关文章:

java - 生成ansi sql INSERT INTO

java - 我的 Sql 自动增量不起作用正确地增量 3 by 3 条记录

java - Eclipse 在 Package Explorer 中不显示错误

python - 使用 "Next"按钮 Python 进行 Web 抓取

java - 在 jdbc 上工作时,我在 eclipse 中遇到错误

java - 从数据库生成模型类

java - eclipse 编辑器中的问题

java - 强制一个线程使用的映射中的元素不应被另一个线程使用,直到第一个线程完成执行

python - 使用 python 在 selenium 中打开多个选项卡

java - 在我的应用程序中检索地理位置远不如 Google map 应用程序在我的设备上同时显示的准确