如何从类路径加载ChromeDriver二进制文件?

时间:2022-09-25 10:54:34

A number of sources make reference to loading the ChromeDriver binary from the classpath, but I haven't worked out how to do it if the binary is not in the classpath root.

许多来源都参考了从类路径加载ChromeDriver二进制文件,但是如果二进制文件不在类路径根目录中,我还没有弄清楚如何做。

To specify the path for the binary it seems that you have to set a system property "webdriver.chrome.driver". First I tried:

要指定二进制文件的路径,您似乎必须设置系统属性“webdriver.chrome.driver”。首先我试过:

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

But I got an error, and it seems it was looking for the driver in the location "C:\<working directory of my application process>\drivers\Chrome\chromedriver.exe". Here the working directory was actually the directory where my source code is stored.

但是我收到了一个错误,似乎是在“C:\ <我的应用程序进程的工作目录> \ drivers \ Chrome \ chromedriver.exe”中查找驱动程序。这里的工作目录实际上是我的源代码存储的目录。

Then I tried:

然后我尝试了:

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

However the same thing happened - this time it was looking in "C:\drivers\Chrome\chromedriver.exe".

然而同样的事情发生了 - 这次是在查看“C:\ drivers \ Chrome \ chromedriver.exe”。

How do I get ChromeDriver to look for the ChromeDriver binary on the classpath when using the "webdriver.chrome.driver" property or any other way of configuring it?

在使用“webdriver.chrome.driver”属性或其他任何配置方式时,如何让ChromeDriver在类路径中查找ChromeDriver二进制文件?

3 个解决方案

#1


Ultimately, I found that ChromeDriver doesn't support classpath-relative access to its binary. However, you can convert the classpath-relative string to a system path and then directly load it, bypassing the system property.

最终,我发现ChromeDriver不支持对其二进制文件的类路径相对访问。但是,您可以将类路径相对字符串转换为系统路径,然后直接加载它,绕过系统属性。

URL url = this.getClass().getClassLoader().getResource(classpathRelativeLocation);
File file = new File(url.getFile()); // Strangely, URL.getFile does not return a File
ChromeDriverService.Builder bldr = (new ChromeDriverService.Builder())
                                   .usingDriverExecutable(file)
                                   .usingAnyFreePort();
ChromeDriver driver = new ChromeDriver(bldr.build());

#2


If you have the driver viz. chromedriver.exe on your classpath, you can use the classloader to load the resource and then get the path to it. The same can be passed in as the second argument to setProperty. For e.g. I have the file chromedriver.exe placed in Java Build Path source folder /src/main/resources

如果你有司机即。在类路径上的chromedriver.exe,您可以使用类加载器加载资源,然后获取它的路径。同样可以作为setProperty的第二个参数传入。对于例如我将文件chromedriver.exe放在Java Build Path源文件夹/ src / main / resources中

I am then able to use the below code to pick the driver from the classpath

然后我可以使用下面的代码从类路径中选择驱动程序

public class App 
{
private static final String CHROME_DRIVER_PATH=App.class.getClassLoader().getResource("chromedriver.exe").getPath(); 
    public static void main( String[] args ) throws InterruptedException
    {
        System.setProperty("webdriver.chrome.driver", CHROME_DRIVER_PATH);
        WebDriver driver=new ChromeDriver();
        Thread.sleep(2000);
        driver.close();        
    }
}

#3


In my opinion, it should work with the following full path:

在我看来,它应该使用以下完整路径:

 System.SetProperty("webdriver.chrome.driver", @"D:/drivers/Chrome/chromedriver.exe");
 WebDriver driver = new ChromeDriver();              
 driver.get("http://www.google.com");

I hope that that this helps you!

我希望这对你有所帮助!

#1


Ultimately, I found that ChromeDriver doesn't support classpath-relative access to its binary. However, you can convert the classpath-relative string to a system path and then directly load it, bypassing the system property.

最终,我发现ChromeDriver不支持对其二进制文件的类路径相对访问。但是,您可以将类路径相对字符串转换为系统路径,然后直接加载它,绕过系统属性。

URL url = this.getClass().getClassLoader().getResource(classpathRelativeLocation);
File file = new File(url.getFile()); // Strangely, URL.getFile does not return a File
ChromeDriverService.Builder bldr = (new ChromeDriverService.Builder())
                                   .usingDriverExecutable(file)
                                   .usingAnyFreePort();
ChromeDriver driver = new ChromeDriver(bldr.build());

#2


If you have the driver viz. chromedriver.exe on your classpath, you can use the classloader to load the resource and then get the path to it. The same can be passed in as the second argument to setProperty. For e.g. I have the file chromedriver.exe placed in Java Build Path source folder /src/main/resources

如果你有司机即。在类路径上的chromedriver.exe,您可以使用类加载器加载资源,然后获取它的路径。同样可以作为setProperty的第二个参数传入。对于例如我将文件chromedriver.exe放在Java Build Path源文件夹/ src / main / resources中

I am then able to use the below code to pick the driver from the classpath

然后我可以使用下面的代码从类路径中选择驱动程序

public class App 
{
private static final String CHROME_DRIVER_PATH=App.class.getClassLoader().getResource("chromedriver.exe").getPath(); 
    public static void main( String[] args ) throws InterruptedException
    {
        System.setProperty("webdriver.chrome.driver", CHROME_DRIVER_PATH);
        WebDriver driver=new ChromeDriver();
        Thread.sleep(2000);
        driver.close();        
    }
}

#3


In my opinion, it should work with the following full path:

在我看来,它应该使用以下完整路径:

 System.SetProperty("webdriver.chrome.driver", @"D:/drivers/Chrome/chromedriver.exe");
 WebDriver driver = new ChromeDriver();              
 driver.get("http://www.google.com");

I hope that that this helps you!

我希望这对你有所帮助!