Jcmoney1010
Jcmoney1010

Reputation: 922

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". 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.

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. 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:

enter image description here

enter image description here

Upvotes: 4

Views: 1608

Answers (2)

harshavmb
harshavmb

Reputation: 3902

Your src package containing .java class is wrong. It should be org.openqa.selenium.example rather 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.

Upvotes: 2

James Dunn
James Dunn

Reputation: 8284

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:

[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.

Solution 3: Declare your own package, like package com.jcmoney.example;, and then create a matching directory in your project com/jcmoney/example and put your Example.java file in it.

EDIT:

Based on the comments below and the screenshot added to the question:

Project Structure

See how it says "Source Packages", and below it "example" and then below it "Example.java"? Example.java is the file that your code is in. It is in the package "example".

So, quickest solution: Change your package declaration from "org.openqa.selenium.example" to just "example".

Upvotes: 3

Related Questions