简体   繁体   English

Selenium Webdriver示例问题

[英]Selenium Webdriver Example issue

I'm trying to follow along with the examples found on https://code.google.com/p/selenium/wiki/GettingStarted but I'm running into an issue with the package org.openqa.selenium.example being "incorrect". 我正在尝试按照https://code.google.com/p/selenium/wiki/GettingStarted上的示例进行操作,但我遇到的问题是org.openqa.selenium.example包“不正确” ”。 The rest of the code seems to be ok with the exception of the public class Example also having a red dot saying it needs to be declared, but I figured this is because the above package is having issues. 除了public class Example之外,代码的其余部分似乎没问题。 public class Example还有一个红点说它需要声明,但我认为这是因为上面的包有问题。

When I run the code, this is the out put: 当我运行代码时,这是输出:

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) 

I know there is a similar thread found here : Can't run Java example for Selenium / WebDriver , but with this being my first go at using both Java and Selenium, I'm still having a hard time trouble shooting this issue. 我知道这里有一个类似的线程: 不能为Selenium / WebDriver运行Java示例 ,但是这是我第一次使用Java和Selenium,我仍然很难解决这个问题。 In case you don't want to follow the link to the example, here is my code: 如果您不想按照示例的链接,这是我的代码:

/*
 * 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();
    }
}

EDIT: 编辑:

在此输入图像描述

在此输入图像描述

The package declaration is the package that your class Example is supposed to be in. It has to match the directory that your code is in, or else it won't compile. 包声明是您的类示例应该包含的包。它必须匹配您的代码所在的目录,否则它将无法编译。

Solution 1: Put your Example.java file should be in a directory like this: 解决方案1:将您的Example.java文件放在如下目录中:

[directory-to-your-project-root-or-source-folder]/org/openqa/selenium/example

Solution 2: Assuming your Example.java is already sitting directly in the root folder of your project, you can just remove the package declaration from the top of your Example.java file. 解决方案2:假设您的Example.java已经直接位于项目的根文件夹中,您只需从Example.java文件的顶部删除包声明即可。

Solution 3: Declare your own package, like package com.jcmoney.example; 解决方案3:声明自己的包,如package com.jcmoney.example; , and then create a matching directory in your project com/jcmoney/example and put your Example.java file in it. ,然后在项目com/jcmoney/example创建一个匹配的目录,并将Example.java文件放入其中。

EDIT: 编辑:

Based on the comments below and the screenshot added to the question: 根据以下评论和添加到问题的屏幕截图:

项目结构

See how it says "Source Packages", and below it "example" and then below it "Example.java"? 看看它如何说“源包”,在它下面的“示例”,然后在它下面的“Example.java”? Example.java is the file that your code is in. It is in the package "example". Example.java是您的代码所在的文件。它位于包“example”中。

So, quickest solution : Change your package declaration from "org.openqa.selenium.example" to just "example". 因此, 最快的解决方案 :将包装声明从“org.openqa.selenium.example”更改为“example”。

Your src package containing .java class is wrong. 您的包含.java类的src包是错误的。 It should be org.openqa.selenium.example rather example . 它应该是org.openqa.selenium.example而不是示例 As the package declared inside class is not same as package declared outside. 由于在类中声明的包与在外部声明的包不同。 It threw a compile time error. 它引发了编译时错误。 Refactoring packaging to org.openqa.selenium.example outside in the src package or editing the org.openqa.selenium.example to example should fix the issue. 重构包装在src外面包org.openqa.selenium.example或编辑org.openqa.selenium.example 示例应该可以解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM