Eclipse + Java + Selenium creates ClassNotFoundException


Eclipse + Java + Selenium creates ClassNotFoundException



just getting started with Eclipse + Java + Selenium.



Not doing anything fancy -



My code looks like this:


WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");



When I run, I get a ClassNotFoundException.



I have selenium-java-2.48.2.jar in my Java Build Path.



What am I missing?





Can you add your stacktrace, so I could see the full exception messge.
– Indrek Ots
Dec 11 '15 at 16:20





Launcher$AppClassLoader(ClassLoader).loadClass(String, boolean) line: not available Launcher$AppClassLoader.loadClass(String, boolean) line: not available Launcher$AppClassLoader(ClassLoader).loadClass(String) line: not available TestApp.main(String) line: 9
– user306517
Dec 11 '15 at 16:24






the class it cannot find is "ChromeDriver"
– user306517
Dec 11 '15 at 16:25





5 Answers
5



You need download Chromedriver first, once you download it, place executable inside your project and set chromedriver property, it should be something like:



project_folder
- src
- chromedriver.exe


System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
WebDriver driver = new ChromeDriver();





Tried all that, didn't help: public class TestApp { public static void main(String args) { System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("google.com"); } }
– user306517
Dec 11 '15 at 18:51






Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function at com.test.TestApp.main(TestApp.java:10) Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 1 more
– user306517
Dec 11 '15 at 18:54





Is there test runner in place, JUnit or TestNG?
– Slav Kurochkin
Dec 11 '15 at 18:58





I see now, you missing test runner (just use JUnit or TestNG) and annotate your test methods with @Test tag, also keep your tests in src/test not in src/main.
– Slav Kurochkin
Dec 11 '15 at 19:09





okay, so I am going to install JUNIT based on junit.sourceforge.net, unzip junit*.jar to some directory, setup environment variable CLASSPATH to include that.
– user306517
Dec 11 '15 at 19:59



String WEB_DRIVER_PATH="";
String url=URL_TO_HIT_ON_BROWSER;

System.setProperty("webdriver.chrome.driver", WEB_DRIVER_PATH);
driver=new ChromeDriver();
driver.get(url +"/");



use updated jars.



try this:


WebDriver driver = new ChromeDriver();

@Test
public void goToWebsite() {
driver.get("http://www.google.com");
}



and then run goToWebsite



I am not sure if you are using 2.XX version of selenium because the problem you are facing is the same i faced when i moved my existing scripts from selenium 2 to Selenium 3.
You need to set the path of the chromedriver first.
Steps-
1. Download chromedriver.exe compatible for your environment.
2. Set the path of chromedriver.Add the following line of code before making ChromeDriver object.


System.setProperty("webdriver.chrome.driver", "chromedriver.exe");



Check if your class file is in a source folder. If it is not, right click on the folder, choose Build Path > Use as source folder



Make sure all your jars are in the build path. I also suggest that you use Maven to help you organize jars as dependencies



After these, you must download the chromedriver.exe file from here:
http://chromedriver.chromium.org/downloads



Place the chromedriver.exe file in any accessible directory and set the web driver environment property by inserting this line on your code before instantiating your driver:


System.setProperty("webdriver.chrome.driver", "<pathToYourDriver>/chromedriver.exe");






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV