Java SeleniumWebDriver示例问题
我试图遵循上的示例,但我遇到了一个问题,即包Java SeleniumWebDriver示例问题,java,selenium,Java,Selenium,我试图遵循上的示例,但我遇到了一个问题,即包org.openqa.selenium.example不正确。剩下的代码似乎还可以,除了公共类示例还有一个红点表示需要声明,但我认为这是因为上面的包有问题 当我运行代码时,这是输出: Error: Could not find or load main class test.Test /Users/me/Library/Caches/NetBeans/8.1/executor-snippets/run.xml:53: Java returned: 1
org.openqa.selenium.example
不正确。剩下的代码似乎还可以,除了公共类示例
还有一个红点表示需要声明,但我认为这是因为上面的包有问题
当我运行代码时,这是输出:
Error: Could not find or load main class test.Test
/Users/me/Library/Caches/NetBeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 3 seconds)
我知道这里有一个类似的线程:,但由于这是我第一次同时使用Java和Selenium,我仍然很难解决这个问题。如果您不想遵循示例的链接,以下是我的代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.openqa.selenium.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Example {
public static void main(String[] args) {
// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new HtmlUnitDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
}
编辑:
包声明是类示例应该包含的包。它必须匹配代码所在的目录,否则它将无法编译 解决方案1:将您的Example.java文件放在如下目录中:
[项目根目录或源文件夹目录]/org/openqa/selenium/example
解决方案2:假设Example.java已经直接位于项目的根文件夹中,您可以从Example.java文件的顶部删除包声明
解决方案3:声明您自己的包,如package com.jcmoney.example
,然后在项目com/jcmoney/example
中创建一个匹配的目录,并将example.java文件放入其中
编辑:
根据以下评论和问题中添加的屏幕截图:
看看它怎么说“源代码包”,下面是“示例”,下面是“example.java”?java是代码所在的文件。它在包“示例”中
因此,最快解决方案:将包声明从“org.openqa.selenium.example”更改为“example”。包含.java类的src包是错误的。它应该是org.openqa.selenium.example而不是example。因为类内声明的包与类外声明的包不同。它抛出了一个编译时错误。在src包中将包重构到org.openqa.selenium.example外部,或者将org.openqa.selenium.example编辑到example应该可以解决这个问题。我能够运行这段代码。我得到了这个输出“页面标题是:奶酪!-谷歌搜索”嗯,你采取了什么步骤?对于我来说,Example.jar几乎不存在,因为我无法在随教程下载的
Selenium server standalone.jar
中找到它。我找错地方了吗?你是从cmd提示符还是ide编译的???我没有在类路径中添加任何Example.jar,除了selenium dependenciesI我正在使用NetBeans IDE,并且我已经将selenium jar文件添加到PATHCLASS中。我不熟悉NetBeans。我使用eclipse。正如TJames所说,添加项目目录和依赖项屏幕截图。这可能有助于我解决问题。这似乎是问题的一部分,我似乎找不到这个Example.jar
文件。我想它应该在我下载的selenium-see-standalone-2
文件中,但我在任何地方都看不到它。不是Example.jar
,Example.java
Example.java
是您在上面的问题中粘贴的包含代码的文件。或者至少应该是这样。您放入示例类代码的文件的名称是什么?如果它没有命名为Example.java
,那么我们就遇到了更多的问题。这是我的错误,我在重播您的帖子和上面的评论时错误地键入了.jar
,而不是.java
。我无法找到Example.java
文件。当我把包声明从顶部拿开时,它会出现一个红色的气泡,说class,interface,enum expected
,这很奇怪。你用的是什么IDE?日食?不管是哪一个,你能在你的问题中添加一个项目目录的屏幕截图吗?我已经用一个屏幕截图进行了更新,我正在使用NetBeans,我对它不是很熟悉。