1

I'm trying to launch firefox browser from eclipse using selenium as I'm learning selenium.

My tutor wrote the below code but when i'M trying the same code I get this exception-

Exception in thread "main" java.lang.IllegalStateException:

The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see

Link1. The latest version can be downloaded from

Link2

Code:

package appselenium1;

import org.openqa.selenium.firefox.FirefoxDriver;

public class A {


public static void main(String[] args) {


    FirefoxDriver driver = new FirefoxDriver();
    driver.get("http://www.gmail.com");

    }

    }
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
Ranjith93
  • 33
  • 1
  • 6

2 Answers2

0

You are facing this exception, because you have not used gecko driver , which is required for launching and sending command in selenium.

You can download the latest version of gecko version from here

Try this :

package appselenium1;

import org.openqa.selenium.firefox.FirefoxDriver;

public class A {

static WebDriver driver ; 

public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Downloads\\geckodriver-v0.20.1-win64\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("http://www.gmail.com");
}
}  
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • I tried the above code but i got this exception-Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/exec/Executor – Ranjith93 Jul 19 '18 at 12:18
  • Did you download the latest version of gecko driver which is require for FF? – cruisepandey Jul 19 '18 at 12:19
  • I downloaded gecko driver and I ran it.But Then I got the following exception-Exception in thread "main" java.lang.NoClassDefFoundError: – Ranjith93 Jul 19 '18 at 12:38
  • Can you tell me how to upload a screenshot into this comment. I've taken a screenshot. – Ranjith93 Jul 19 '18 at 12:42
  • Can you check this https://stackoverflow.com/questions/17973970/how-to-solve-java-lang-noclassdeffounderror , and also tell us how are you executing your code, with the modification which I suggested. – cruisepandey Jul 19 '18 at 12:49
  • I've uploaded the screenshot check out the screenshot- – Ranjith93 Jul 19 '18 at 12:50
  • @Ranjith93 :Can you check the path C:\\Download\\geckodriver – cruisepandey Jul 19 '18 at 12:50
  • and I do not see any package , Please create one package like the one I have suggested in my answer. – cruisepandey Jul 19 '18 at 12:52
  • [1]: https://i.stack.imgur.com/rQoB4.png-This is the changes that i made on eclipse – Ranjith93 Jul 19 '18 at 13:06
  • [2]: https://i.stack.imgur.com/GFiL6.png- This is the file path for gecko – Ranjith93 Jul 19 '18 at 13:07
  • `NoClassDefFoundError` indicates that the classloader which is responsible for dynamically loading classes, cannot find the .class file for the class that you're trying to use. – cruisepandey Jul 19 '18 at 13:13
  • This exception has nothing to do with selenium. There is something while compiling your java class , I have found one link , go through that that may resolve your problem https://stackoverflow.com/questions/17973970/how-to-solve-java-lang-noclassdeffounderror – cruisepandey Jul 19 '18 at 13:14
  • How do i launch browser without using gecko.Do i have to use older version of selenium.The link which you sent me above was not of much help i'm just a student, can you help me with it. @ cruisepandey – Ranjith93 Jul 19 '18 at 14:40
  • Yes you will have to downgrade selenium version along with browser degradation , I believe that will do. Don't worry slowly you would learn. – cruisepandey Jul 19 '18 at 14:42
0

While you work with Selenium ver3.x, GeckoDriver ver0.21.0 and Firefox ver61.0.1, you need to download the latest GeckoDriver from mozilla/geckodriver and store it anywhere within your system. In your code you need to provide the absolute path of the GeckoDriver through the System.setProperty() line as follows:

package demo;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class A_Firefox 
{
    public static void main(String[] args) 
    {
        System.setProperty("webdriver.gecko.driver", "C:/path/to/geckodriver.exe");
        WebDriver driver =  new FirefoxDriver();
        driver.get("http://www.gmail.com");
        System.out.println("Page Title is : "+driver.getTitle());
        driver.quit();
    }
}

Note: Replace the packagename (demo in this example) with your own package name.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352