无法通过 Heroku 中的 Selenium webdriver(Java) 调用无头 chrome 驱动程序
Unable to invoke headless chrome driver through Selenium webdriver(Java) in Heroku
我在 heroku 环境中调用无头 chrome 并在 WINDOWS(本地计算机)中完美运行时遇到问题。
错误:
2018-02-07T05:37:22.412428+00:00 heroku[web.1]: Starting process with command `java -cp target/classes:target/dependency/* com.appirio.sd.TestScript` 2018-02-07T05:37:24.211467+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2018-02-07T05:37:24.219616+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx300m -Xss512k -Dfile.encoding=UTF-8
2018-02-07T05:37:24.381460+00:00 app[web.1]: Invoke Browser
2018-02-07T05:37:24.503816+00:00 app[web.1]: Path: /app/.apt/usr/bin/google-chrome-stable
2018-02-07T05:37:24.503854+00:00 app[web.1]: Driver Path: agent//chromedriver
2018-02-07T05:37:24.650636+00:00 app[web.1]: Exception in thread "main" java.lang.IllegalStateException: The driver is not executable: /app/agent/chromedriver
2018-02-07T05:37:24.650644+00:00 app[web.1]: at com.google.common.base.Preconditions.checkState(Preconditions.java:534)
2018-02-07T05:37:24.650650+00:00 app[web.1]: at org.openqa.selenium.chrome.ChromeDriverService.access[=12=]0(ChromeDriverService.java:32)
2018-02-07T05:37:24.650647+00:00 app[web.1]: at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:140)
2018-02-07T05:37:24.650655+00:00 app[web.1]: at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:339)
2018-02-07T05:37:24.650649+00:00 app[web.1]: at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:131)
2018-02-07T05:37:24.650653+00:00 app[web.1]: at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)
2018-02-07T05:37:24.650657+00:00 app[web.1]: at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
2018-02-07T05:37:24.650658+00:00 app[web.1]: at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:157)
2018-02-07T05:37:24.650660+00:00 app[web.1]: at com.appirio.commands.Selenium.launchBrowser(Selenium.java:42)
2018-02-07T05:37:24.650663+00:00 app[web.1]: at com.appirio.sd.TestScript.main(TestScript.java:12)
2018-02-07T05:37:24.733915+00:00 heroku[web.1]: State changed from starting to crashed
2018-02-07T05:37:24.717297+00:00 heroku[web.1]: Process exited with status 1
代码片段:
public WebDriver launchBrowser(){
String driverPath="";
if(getOS().equals(OS.WINDOWS)){
driverPath="agent//chromedriver.exe";
}else if(getOS().equals(OS.LINUX)){
driverPath="agent//chromedriver";
}
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("window-size=1200x600");
if(getOS().equals(OS.LINUX)){
try{ //GOOGLE_CHROME_SHIM GOOGLE_CHROME_BIN
String binaryPath=EnvironmentUtils.getProcEnvironment().get("GOOGLE_CHROME_SHIM");
System.out.println("Path: "+binaryPath);
options.setBinary(binaryPath);
options.addArguments("--disable-gpu");
options.addArguments("--no-sandbox");
}catch(Exception e){
}
}
System.out.println("Driver Path: "+driverPath);
System.setProperty("webdriver.chrome.driver", driverPath);
WebDriver driver=new ChromeDriver(options);
return driver;
}
Heroku 是 Linux 环境,因此,很少有条件专门为 Linux 环境添加。
提前致谢!!!
在发布此问题之前和之后,投入了充足的时间来解决此问题。我很高兴地说成功了。
解决方案:
我已指定驱动程序路径(在脚本中)并在 heroku env 中添加了驱动程序构建包。驱动程序路径造成了问题。因此,只需删除以下类似 heroku env 的代码并仅为 windows
添加
System.setProperty("webdriver.chrome.driver", driverPath);
因此,代码看起来像
public WebDriver launchBrowser(){
String driverPath="";
if(getOS().equals(OS.WINDOWS)){
driverPath="agent//chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverPath);
}
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("window-size=1200x600");
if(getOS().equals(OS.LINUX)){
try{ //GOOGLE_CHROME_SHIM GOOGLE_CHROME_BIN
String binaryPath=EnvironmentUtils.getProcEnvironment().get("GOOGLE_CHROME_SHIM");
System.out.println("Path: "+binaryPath);
options.setBinary(binaryPath);
options.addArguments("--disable-gpu");
options.addArguments("--no-sandbox");
}catch(Exception e){
}
}
WebDriver driver=new ChromeDriver(options);
return driver;
}
只是总结一下在 Heroku 中设置 "selenium webdriver java script which invokes Headless chrome" 的整个方法:
Add below buildpacks to hero app
heroku/java
在 heroku 应用程序中部署代码
谢谢大家!
添加构建包的命令。
heroku buildpacks:set heroku/java
heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-google-chrome
heroku buildpacks:add --index 2 https://github.com/heroku/heroku-buildpack-chromedriver
我在 heroku 环境中调用无头 chrome 并在 WINDOWS(本地计算机)中完美运行时遇到问题。
错误:
2018-02-07T05:37:22.412428+00:00 heroku[web.1]: Starting process with command `java -cp target/classes:target/dependency/* com.appirio.sd.TestScript` 2018-02-07T05:37:24.211467+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2018-02-07T05:37:24.219616+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx300m -Xss512k -Dfile.encoding=UTF-8
2018-02-07T05:37:24.381460+00:00 app[web.1]: Invoke Browser
2018-02-07T05:37:24.503816+00:00 app[web.1]: Path: /app/.apt/usr/bin/google-chrome-stable
2018-02-07T05:37:24.503854+00:00 app[web.1]: Driver Path: agent//chromedriver
2018-02-07T05:37:24.650636+00:00 app[web.1]: Exception in thread "main" java.lang.IllegalStateException: The driver is not executable: /app/agent/chromedriver
2018-02-07T05:37:24.650644+00:00 app[web.1]: at com.google.common.base.Preconditions.checkState(Preconditions.java:534)
2018-02-07T05:37:24.650650+00:00 app[web.1]: at org.openqa.selenium.chrome.ChromeDriverService.access[=12=]0(ChromeDriverService.java:32)
2018-02-07T05:37:24.650647+00:00 app[web.1]: at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:140)
2018-02-07T05:37:24.650655+00:00 app[web.1]: at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:339)
2018-02-07T05:37:24.650649+00:00 app[web.1]: at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:131)
2018-02-07T05:37:24.650653+00:00 app[web.1]: at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)
2018-02-07T05:37:24.650657+00:00 app[web.1]: at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
2018-02-07T05:37:24.650658+00:00 app[web.1]: at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:157)
2018-02-07T05:37:24.650660+00:00 app[web.1]: at com.appirio.commands.Selenium.launchBrowser(Selenium.java:42)
2018-02-07T05:37:24.650663+00:00 app[web.1]: at com.appirio.sd.TestScript.main(TestScript.java:12)
2018-02-07T05:37:24.733915+00:00 heroku[web.1]: State changed from starting to crashed
2018-02-07T05:37:24.717297+00:00 heroku[web.1]: Process exited with status 1
代码片段:
public WebDriver launchBrowser(){
String driverPath="";
if(getOS().equals(OS.WINDOWS)){
driverPath="agent//chromedriver.exe";
}else if(getOS().equals(OS.LINUX)){
driverPath="agent//chromedriver";
}
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("window-size=1200x600");
if(getOS().equals(OS.LINUX)){
try{ //GOOGLE_CHROME_SHIM GOOGLE_CHROME_BIN
String binaryPath=EnvironmentUtils.getProcEnvironment().get("GOOGLE_CHROME_SHIM");
System.out.println("Path: "+binaryPath);
options.setBinary(binaryPath);
options.addArguments("--disable-gpu");
options.addArguments("--no-sandbox");
}catch(Exception e){
}
}
System.out.println("Driver Path: "+driverPath);
System.setProperty("webdriver.chrome.driver", driverPath);
WebDriver driver=new ChromeDriver(options);
return driver;
}
Heroku 是 Linux 环境,因此,很少有条件专门为 Linux 环境添加。
提前致谢!!!
在发布此问题之前和之后,投入了充足的时间来解决此问题。我很高兴地说成功了。
解决方案:
我已指定驱动程序路径(在脚本中)并在 heroku env 中添加了驱动程序构建包。驱动程序路径造成了问题。因此,只需删除以下类似 heroku env 的代码并仅为 windows
添加 System.setProperty("webdriver.chrome.driver", driverPath);
因此,代码看起来像
public WebDriver launchBrowser(){
String driverPath="";
if(getOS().equals(OS.WINDOWS)){
driverPath="agent//chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverPath);
}
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("window-size=1200x600");
if(getOS().equals(OS.LINUX)){
try{ //GOOGLE_CHROME_SHIM GOOGLE_CHROME_BIN
String binaryPath=EnvironmentUtils.getProcEnvironment().get("GOOGLE_CHROME_SHIM");
System.out.println("Path: "+binaryPath);
options.setBinary(binaryPath);
options.addArguments("--disable-gpu");
options.addArguments("--no-sandbox");
}catch(Exception e){
}
}
WebDriver driver=new ChromeDriver(options);
return driver;
}
只是总结一下在 Heroku 中设置 "selenium webdriver java script which invokes Headless chrome" 的整个方法:
Add below buildpacks to hero app
heroku/java
在 heroku 应用程序中部署代码
谢谢大家!
添加构建包的命令。
heroku buildpacks:set heroku/java
heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-google-chrome
heroku buildpacks:add --index 2 https://github.com/heroku/heroku-buildpack-chromedriver