我已经安装了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 中,我执行了以下步骤:
- 右键单击“UsingGoogleSearchPage.java”
- 单击“运行方式”->“Java 应用程序”
程序没有执行。发生错误(请参见图片)。
最佳答案
我认为您对 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/