繁体   English   中英

Selenium Webdriver示例问题

[英]Selenium Webdriver Example issue

我正在尝试按照https://code.google.com/p/selenium/wiki/GettingStarted上的示例进行操作,但我遇到的问题是org.openqa.selenium.example包“不正确” ”。 除了public class Example之外,代码的其余部分似乎没问题。 public class 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) 

我知道这里有一个类似的线程: 不能为Selenium / WebDriver运行Java示例 ,但是这是我第一次使用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文件放在如下目录中:

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

解决方案2:假设您的Example.java已经直接位于项目的根文件夹中,您只需从Example.java文件的顶部删除包声明即可。

解决方案3:声明自己的包,如package com.jcmoney.example; ,然后在项目com/jcmoney/example创建一个匹配的目录,并将Example.java文件放入其中。

编辑:

根据以下评论和添加到问题的屏幕截图:

项目结构

看看它如何说“源包”,在它下面的“示例”,然后在它下面的“Example.java”? Example.java是您的代码所在的文件。它位于包“example”中。

因此, 最快的解决方案 :将包装声明从“org.openqa.selenium.example”更改为“example”。

您的包含.java类的src包是错误的。 它应该是org.openqa.selenium.example而不是示例 由于在类中声明的包与在外部声明的包不同。 它引发了编译时错误。 重构包装在src外面包org.openqa.selenium.example或编辑org.openqa.selenium.example 示例应该可以解决这个问题。

暂无
暂无

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

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