Java 无法在 Selenium Webdriver 2 中启动 Chrome 驱动程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23948340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 09:18:09  来源:igfitidea点击:

Unable to start Chrome Driver in Selenium Webdriver 2

javagoogle-chromeseleniumselenium-chromedriver

提问by demouser123

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.

我正在尝试从 Selenium webdriver 打开 Chrome 浏览器,但我没有这样做。起初我尝试从同一个程序打开 Chrome 和 Firefox。Firefox 浏览器运行良好,但出现与 ChromeDriver exe 文件不存在相关的错误。我下载了 ChromeDriver 文件并将其添加到 External Jars 并使用该System.setProperty(方法调用它。

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

我把Chrome部分拆分成一个单独的程序,名为“Chrome_test”,其代码如下

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

错误:无法找到或加载主类 test.selenium.Chrome_Test

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

我检查了类路径变量,一切似乎都到位了。我在这里缺少什么?

采纳答案by sandeep kumar

You better place two backward slashes like:

您最好放置两个反斜杠,例如:

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

It will work.

它会起作用。

回答by user3652502

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

使用反斜杠 (\) 更改 chrome 驱动程序属性行,它会起作用。

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

回答by BullyWiiPlaza

I wrote code which downloads and installs the latest ChromeDriverautomatically to the project root directory if none has been found. That way you can receive a ChromeDriverinstance without actually worrying about the chromedriver.exefile. Feel free to adjust it to your needs. You still need to include Seleniumlibrariesin your project though. For my ChromeDriverFactoryclass below you also need Apache Commons IOand Zip4J.

ChromeDriver如果没有找到,我编写了自动下载并安装最新到项目根目录的代码。这样你就可以接收一个ChromeDriver实例而不必真正担心chromedriver.exe文件。随意调整它以满足您的需求。不过,您仍然需要在项目中包含Selenium。对于我ChromeDriverFactory下面的课程,您还需要Apache Commons IOZip4J

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);
    }
}