无法通过Heroku中的Selenium webdriver(Java)调用无头Chrome驱动程序

Jitendra jangid

在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$000(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 env添加条件。

提前致谢!!!

Jitendra jangid

在发布此问题之前和之后,投入足够的时间来解决此问题。我非常高兴地说成功了。

解:

我已经在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中设置“调用Headless chrome的Selenium Webdriver Java脚本”的整个方法:

将以下构建包添加到英雄应用程序

heroku / java

https://github.com/heroku/heroku-buildpack-google-chrome

https://github.com/heroku/heroku-buildpack-chromedriver

在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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python Selenium Chrome Web驱动程序

如何在使用chrome驱动程序/ firefox驱动程序时更改Webdriver中的文件下载位置

带有Chrome驱动程序的Selenium网格(WebDriverException:驱动程序可执行文件的路径必须由webdriver.chrome.driver系统属性设置)

java.lang.IllegalStateException:必须通过webdriver.chrome.driver系统属性设置驱动程序可执行文件的路径;(driver.get)

Selenium Chrome驱动程序-获取java.lang.IllegalStateException:驱动程序可执行文件的路径必须由webdriver.chrome.driver设置

无法通过Selenium中的htmlunit驱动程序运行脚本

Selenium Webdriver不会退出Chrome驱动程序

Java Selenium Chrome驱动程序-禁用图像加载

无头Chrome Web驱动程序速度太慢,无法下载文件

如何使用Selenium在Python中使用无头驱动程序

无法检查IntelliJ-Selenium Chrome驱动程序中的搜索按钮

如何在没有被拒绝的情况下通过无头驱动程序访问站点

Selenium Chrome驱动程序74.0.3729.6问题

Selenium WebDriver Factory选择相同的驱动程序

Selenium Chrome驱动程序无法选择或输入登录

Selenium Webdriver和Chrome驱动程序-无法运行Chrome驱动程序

无法使用Heroku上的Java驱动程序与MongoDB连接

java.lang.IllegalStateException:必须通过webdriver.chrome.driver系统属性设置驱动程序可执行文件的路径;

Selenium Chrome WebDriver无法在端口上启动驱动程序服务

TestNG Selenium Java -- java.lang.IllegalStateException:驱动程序可执行文件的路径必须由 webdriver.chrome.driver 系统属性设置

webdriver-manager 更新无法下载 Chrome 驱动程序和 update-config.json

将 Chrome 驱动程序与 Selenium Webdriver 一起使用时出现 SocketException

Selenium:-java.lang.IllegalStateException: 驱动程序可执行文件的路径必须由 webdriver.chrome.driver 系统属性设置

如何使用 selenium chrome 驱动程序在 Python 中按名称过滤网络调用条目?

在函数中调用驱动程序时,如何在 Python3 中保持 Selenium Webdriver 浏览器打开?

在 element selenium python chrome 驱动程序中查找元素

Selenium 通过 Outlook 邮件登录导航(无头驱动程序)

无法通过 xpath 通过 selenium chrome 驱动程序单击 Web 元素

无头运行时 Jmeter 中的 Web 驱动程序采样器没有此类元素错误