在春季启动时加载chromedriver

时间:2020-07-10 21:10:36

标签: spring spring-boot selenium web-applications automated-tests

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

我尝试过这没用。尝试将文件路径设置为“ resources / chromedriver.exe”不能正常工作

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

2 个答案:

答案 0 :(得分:2)

如果您使用的是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);

答案 1 :(得分:0)

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