如何使用 node.js(Firefox 或 Chrome)在 Heroku 上运行 Selenium-Webdriver

2023-12-09

我想在 Heroku 上使用 Selenium 和 Firefox 或其他能够显示视频的浏览器。不幸的是,PhantomJS 不起作用,因为浏览器无法播放视频。


我已经通过使用这些构建包在 Firefox 中进行了尝试:

  • https://github.com/buitron/firefox-buildpack
  • https://github.com/buitron/geckodriver-buildpack

并通过使用这些 buildpacks 来处理 chrome:

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

但我总是在 heroku-logs 中得到这个:

2019-09-20T15:04:47.000000+00:00 app[api]: Build succeeded
2019-09-20T15:04:49.118915+00:00 app[web.1]: Error: Server terminated early with status 2
2019-09-20T15:04:49.118934+00:00 app[web.1]: at earlyTermination.catch.e (/app/node_modules/selenium-webdriver/remote/index.js:251:52)
2019-09-20T15:04:49.118936+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:68:7)

有没有办法在heroku上使用selenium和可以显示视频格式的浏览器,比如youtube上的视频?

我还没有找到适合我的解决方案。


UPDATE

如果我尝试这个answser将显示相同的错误:

const chrome = require('selenium-webdriver/chrome');

let options = new chrome.Options();

options.addArguments('--headless');
options.addArguments('--disable-gpu');
options.addArguments('--no-sandbox');

let driver = new webdriver.Builder()
  .forBrowser('chrome')
  .setChromeOptions(options)
  .build();

driver.get('http://www.google.com').catch(err => console.log(err));

我尝试了这个并且成功了。

注意:我在 chrome 中使用 React、Express 和 Selenium

第 1 步:创建一个新的 Heroku 应用程序。

第 2 步:从您的终端,使用以下命令登录 Herokuheroku login

第 3 步:登录后,cd到您的项目目录并将其远程设置为您的heroku应用程序。heroku git:remote -a YOUR-HEROKU-APP-NAME

第 4 步:在终端中运行以下所有命令

heroku buildpacks:add https://github.com/heroku/heroku-buildpack-chromedriver
heroku buildpacks:add https://github.com/heroku/heroku-buildpack-google-chrome
heroku config:set CHROME_DRIVER_PATH=/app/.chromedriver/bin/chromedriver
heroku config:set CHROME_BINARY_PATH=/app/.apt/opt/google/chrome/chrome

第 5 步:从浏览器登录 heroku 并导航到您的应用程序。转到设置并在下面buildpacks, add heroku/nodejs

第 6 步:这就是我的 index.js 的样子。注意:我的快速入口点在里面root-dir/server/index.js我的反应文件在里面root-dir/client/

const express = require('express');
const app = express();
const path = require('path');

// Serve static files from the React app. 
app.use(express.static(path.join(__dirname, '..', 'client/build')));


app.get('/api', async (req, res) => {
    const webdriver = require('selenium-webdriver');
    require('chromedriver');
    const chrome = require('selenium-webdriver/chrome');

    let options = new chrome.Options();
    options.setChromeBinaryPath(process.env.CHROME_BINARY_PATH);
    let serviceBuilder = new chrome.ServiceBuilder(process.env.CHROME_DRIVER_PATH);
    
    //Don't forget to add these for heroku
    options.addArguments("--headless");
    options.addArguments("--disable-gpu");
    options.addArguments("--no-sandbox");
  

    let driver = new webdriver.Builder()
        .forBrowser('chrome')
        .setChromeOptions(options)
        .setChromeService(serviceBuilder)
        .build();

    await driver.get('http://www.google.com');
    res.send(await driver.getTitle());
});

app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, '..', 'client/build/index.html'));
});

const port = process.env.PORT || 5000;
app.listen(port, () => {
    console.log(`listening to port ${port} now...`);
});

第 7 步(如果您使用的是 React):现在在您的package.json in root-dir/,添加这个

"scripts": {
...
"heroku-postbuild": "cd client && npm install && npm run build"
}

第 8 步(如果您使用的是 React):在您的package.json in root-dir/client/ (i.e: package.json对于反应应用程序),添加以下行:

  "proxy": "http://localhost:5000/",

第8步:(如果你使用的是react):里面root-dir/client/src/,创建一个新文件,名为setupProxy.js并粘贴以下代码:

const proxy = require("http-proxy-middleware");

module.exports = function(app) {
    app.use(proxy('/api', { target: `http://localhost:${process.env.PORT || 5000}/`}));
};

步骤 9: 现在,您已准备好进行部署。确保您已安装以下软件包:express, selenium-webdriver, and chromedriver

第10步:现在将其推送到heroku

git add .
git commit -m "my app"
git push heroku master
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 node.js(Firefox 或 Chrome)在 Heroku 上运行 Selenium-Webdriver 的相关文章

随机推荐

  • 需要工作表脚本将图像保存到驱动器

    我需要一个与 google 工作表一起使用的脚本 以将 img url 列表保存到特定的 google 驱动器文件夹 并使用来自另一个单元格的文件命名 例如 A 列 文件 URL 图像文件路径 jpgB 列 另存为名称 image 1 自动
  • Javascript-按值删除数组项[重复]

    这个问题在这里已经有答案了 我的情况 var id tag 1 2 3 78 5 6 7 8 47 34 90 我想delete where id tag 90并返回 var id tag 1 2 3 78 5 6 7 8 47 34 我怎
  • 数据框中连续出现的情况

    我有上面的数据框包含不同的测量值 我想确定连续测量值 长度大小大于或等于 6 w一次拍摄t 例如 在以下情况id 1 from t3 t8有6连续的w记录的措施 我想将结果保存到2个数据框中 df1 At least 6 consecuti
  • iOS NSDateFormatter 需要 NSLocale 即使它是 UTC

    我怀疑我无法理解为什么会这样 我向这个网站的诸神求助 我有一个约会是这样的 1982 01 01T00 00 00Z 当我显示服务器发送的任何内容时 我知道 客户要求 不是好的做法 我强制设备使用以下方法具有该时区 在没有错误检查的情况下进
  • 很困惑将代码放入 AsyncTask 中

    我有这个代码来接收聊天消息 但当我试图把它放进去时我很困惑AsyncTask 我有警告read cannot be resolved for read readline 当我把它放进去时postexecute 我想让这段代码在后台工作 以检
  • 不可能将对象添加到可变数组

    我正在尝试将 Song 对象添加到可变数组中 但我很困惑 因为尽管添加了对象 但数组的计数并未增加 Song h import
  • Python 中的 cURL 帮助

    我必须向服务器发送请求 在该网站的 API 文档中有一个在 PHP 中使用 cURL 的示例 ch curl init curl setopt ch CURLOPT URL http api website com curl setopt
  • 在 HTML 中呈现任意 JSON

    我正在编写一个数据查看器页面来呈现从服务器作为 JSON 发送的对象 JSON 对象的内容和复杂性各不相同 从具有少量属性的平面对象到具有多层嵌套和数组字段的较大结构 我想做的是渲染对象的简单表示 可能作为 ul 从那里我可以添加一些东西来
  • 将两个 Unity 项目导入 Android Studio 无法按预期工作

    我目前正在接受培训 制作使用 Unity 添加一些功能 AR VR 等 的应用程序 目前我一直在使用 Android Studio 开发 Android 一旦完成 我将在 iOS 上进行训练 我的目标很简单 我的MainActivity显示
  • 在 Jupyter 实验室中以深色主题绘制 R 几乎不可见

    我尝试在 Jupyter 实验室中使用 R 内核进行绘图 并将 Jupyter 实验室主题设置为 Jupyterlab dark 下面是我的代码 options repr plot width 20 repr plot height 5 p
  • 使用三元表达式返回会在第一次迭代时中断 foreach 循环

    好的 我测试了以下内容 我会让您知道我的发现 echo 1 lt 0 true false will echo true echo 1 gt 0 true false will echo true Notice that 1 and 1 a
  • 在armeabi-v7a中,streqh导致“无效指令,您的意思是:strexh,strh?”。 “eq”去哪儿了?

    我在尝试为 armeabi v7a 编译的一些 ARM 代码中遇到此错误 显然是 v7a 之前的内容 jni 6502asm arm S 108 2 error invalid instruction did you mean strexh
  • 每次列中字符串值发生变化时如何拆分数据框?

    我有一个以下形式的数据框 time value label 0 2020 01 01 0 556014 high 1 2020 01 02 0 185451 high 2 2020 01 03 0 401111 medium 3 2020
  • if 语句如何工作?

    我正在用 Python 制作一款基于文本的俄罗斯轮盘游戏 但我还没有完成 只需查看我的代码就可以看出 usr bin env python print The maximum number of bullets is 3 bulletcou
  • 错误“相机已取消” - App Sencha with PhoneGap

    我正在开发一个应用程序 您可以用手机拍照并将其发送到社交网络 我的应用程序是使用 sencha touch 2 和 Phonegap 开发的 当我尝试拍照时 我的应用程序重新启动并给出错误 相机已取消 我尝试查看 console logs
  • Perl 相当于 (Postgresql BETWEEN 运算符?

    有这样的事吗 相当于a lt expr lt b 或者用 SQL 术语来说expr BETWEEN a AND b where expr只评估一次 或者只是要求这个变得愚蠢 在 Perl 中 有多种方法可以做到这一点 if a lt x a
  • 如何在设定的时间后自动保存并退出工作表? (Excel VBA)

    如果超过 5 分钟没有任何活动 是否有办法让打开的工作表自行关闭 举例来说 我在一张工作表上工作了一段时间 然后打开该工作表走开 20 分钟 网络上的某人需要访问该工作表 但无法访问 因为我正在其中 我希望这样在我离开办公桌超过 5 分钟后
  • 将函数从递归转换为迭代

    我写的这个函数非常慢 因为 php 不能很好地处理递归 我正在尝试将其转换为 while 循环 但我无法理解如何做到这一点 谁能给我一些建议吗 public function findRoute curLoc distanceSoFar e
  • 验证输入框中的用户输入

    当我运行以下代码片段并输入可接受的值时 我得到了所需的结果 do while len strselect 0 or strselect lt gt 1 or strselect lt gt 2 or strselect lt gt 3 st
  • 如何使用 node.js(Firefox 或 Chrome)在 Heroku 上运行 Selenium-Webdriver

    我想在 Heroku 上使用 Selenium 和 Firefox 或其他能够显示视频的浏览器 不幸的是 PhantomJS 不起作用 因为浏览器无法播放视频 我已经通过使用这些构建包在 Firefox 中进行了尝试 https githu