在 spring 引导中加载 chromedriver

load chromedriver in spring boot

我是 运行 一个 spring 引导应用程序,我试图让 chromedriver 不从我的本地目录加载,而是从项目资源文件夹加载。我的 chromdriver.exe 在 resources/chromedriver.exe 中,但我不确定如何加载它。

我试过了,没用。尝试将文件路径设置为“resources/chromedriver.exe”也没有用

  String filePath = ClassLoader.getSystemClassLoader().getResource("resources/chromedriver.exe").getFile();
            System.out.println(filePath);
            System.setProperty("webdriver.chrome.driver", filePath)

如果您正在使用 Spring,您可以试试这个:

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;



Resource resource = new ClassPathResource("chromedriver.exe");
String filePath = resource.getFile().getPath();

System.out.println(filePath);
System.setProperty("webdriver.chrome.driver", filePath);
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
import org.junit.Test;

public class GettingStarted {
  @Test
  public void testGoogleSearch() throws InterruptedException {
    // Optional. If not specified, WebDriver searches the PATH for chromedriver.
    System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

    WebDriver driver = new ChromeDriver();
    driver.get("http://www.google.com/");
    Thread.sleep(5000);  // Let the user actually see something!
    WebElement searchBox = driver.findElement(By.name("q"));
    searchBox.sendKeys("ChromeDriver");
    searchBox.submit();
    Thread.sleep(5000);  // Let the user actually see something!
    driver.quit();
  }
}

https://sites.google.com/a/chromium.org/chromedriver/getting-started

例如在 Windows 中,如果 chromedriver.exe 在 C:/chromium 中,则使用:

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