demouser123
demouser123

Reputation: 4264

Unable to start Chrome Driver in Selenium Webdriver 2

I am trying to open Chrome browser from Selenium webdriver but I'm failing to do so. At first I tried opening both Chrome and Firefox from the same program. The Firefox browser works perfectly fine, while I got error related to ChromeDriver exe file being not present. I downloaded the ChromeDriver file and added that to the External Jars and also called it using the System.setProperty( method.

Here is the original code:

package test.selenium;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Selenium_test {

public static void main(String[] args) {

    FirefoxDriver dr1=new FirefoxDriver();
    FirefoxDriver dr2=new FirefoxDriver();

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

    ChromeDriver dr3=new ChromeDriver();
    ChromeDriver dr4=new ChromeDriver();


    dr1.get("http://google.com");
    dr2.get("http://northeastraveller.com");

    dr3.get("http://quora.com");
    dr4.get("http://facebook.com");
    // TODO Auto-generated method stub

}

}

I separated the Chrome part into a separate program named "Chrome_test", whose code is as follows

package test.selenium;
import org.openqa.selenium.chrome.ChromeDriver;

 public class Chrome_Test{

 public static void main(String[] args) {


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

    ChromeDriver dr3=new ChromeDriver();
    ChromeDriver dr4=new ChromeDriver();

    dr3.get("http://quora.com");
    dr4.get("http://facebook.com");
    // TODO Auto-generated method stub

}

}

Now I'm getting the following error :

Error: Could not find or load main class test.selenium.Chrome_Test

I checked the classpath variables and all seems to be at place. What am I missing here?

Upvotes: 2

Views: 23026

Answers (3)

sandeep kumar
sandeep kumar

Reputation: 195

You better place two backward slashes like:

System.setProperty("webdriver.chrome.driver", "C:\\ChromeDriver\\chromedriver.exe");

It will work.

Upvotes: 2

user3652502
user3652502

Reputation: 1

Change the chrome driver properties line with backslashes (\) and it would work.

System.setProperty("webdriver.chrome.driver", "C:\\ChromeDriver\\chromedriver.exe");

Upvotes: 0

BullyWiiPlaza
BullyWiiPlaza

Reputation: 19223

I wrote code which downloads and installs the latest ChromeDriver automatically to the project root directory if none has been found. That way you can receive a ChromeDriver instance without actually worrying about the chromedriver.exe file. Feel free to adjust it to your needs. You still need to include Selenium libraries in your project though. For my ChromeDriverFactory class below you also need Apache Commons IO and Zip4J.

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ChromeDriverFactory
{
    private static String chromeDriverRepository = "http://chromedriver.storage.googleapis.com/";

    public static WebDriver getChromeDriver() throws MalformedURLException,
            IOException, ZipException
    {
        String chromeDriverFileName = "chromedriver.exe";
        File chromeDriverFile = new File(chromeDriverFileName);

        if (!chromeDriverFile.exists())
        {
            installChromeDriver();
        }

        setChromeDriverProperty(chromeDriverFileName);

        return new ChromeDriver();
    }

    private static void setChromeDriverProperty(String chromeDriverFileName)
    {
        System.setProperty("webdriver.chrome.driver", chromeDriverFileName);
    }

    private static void installChromeDriver() throws IOException,
            MalformedURLException, ZipException
    {
        String newestVersion = getNewestVersion();
        String targetFile = "chromedriver_win32.zip";
        String downloadUrl = chromeDriverRepository + newestVersion + "/"
                + targetFile;
        String downloadFileName = FilenameUtils.getName(downloadUrl);
        File downloadFile = new File(downloadFileName);
        String projectRootDirectory = System.getProperty("user.dir");

        FileUtils.copyURLToFile(new URL(downloadUrl), downloadFile);

        ZipFile zipFile = new ZipFile(downloadFile);
        zipFile.extractAll(projectRootDirectory);
        FileUtils.deleteQuietly(downloadFile);
    }

    private static String getNewestVersion() throws MalformedURLException,
            IOException
    {
        String newestVersionUrl = chromeDriverRepository + "LATEST_RELEASE";
        InputStream input = new URL(newestVersionUrl).openStream();

        return IOUtils.toString(input);
    }
}

Upvotes: 0

Related Questions