【问题标题】:Unable to start Chrome Driver in Selenium Webdriver 2无法在 Selenium Webdriver 2 中启动 Chrome 驱动程序
【发布时间】:2014-05-30 06:22:59
【问题描述】:

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

这是原始代码:

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

}

}

我将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

}

}

现在我收到以下错误:

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

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

【问题讨论】:

  • “错误”表示找不到您的类,这与找不到 chromedriver 无关。您是否创建了档案或其他东西并忘记将您的主类添加到清单中?也许您忘记将本地 ( . ) 路径添加到您的 java 参数中。
  • 你能进一步解释一下吗?我不认为我做过这样的事情。
  • 会因为我使用的是 Win-7 64 位而发生吗?因为google给出的api链接中没有ChromeDriver的64位驱动。

标签: java google-chrome selenium selenium-chromedriver


【解决方案1】:

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

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

它会起作用的。

【讨论】:

  • 这应该在评论中,而不是作为单独的答案
  • 我没有发表评论的声誉。
【解决方案2】:

我编写的代码会自动下载最新的ChromeDriver 并将其安装到项目根目录(如果没有找到)。这样您就可以接收ChromeDriver 实例,而无需真正担心chromedriver.exe 文件。随意调整它以适应您的需求。不过,您仍然需要在项目中包含 Selenium libraries。对于下面我的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);
    }
}

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 抱歉我的代码没有用。看起来还有其他问题。
    猜你喜欢
    • 1970-01-01
    • 2014-03-23
    • 2014-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 2011-09-16
    相关资源
    最近更新 更多