【问题标题】:How to use Selenium Webdriver on Heroku?如何在 Heroku 上使用 Selenium Webdriver?
【发布时间】:2017-08-09 04:46:33
【问题描述】:

我正在开发一个 Node.js 应用程序,并在其上使用 Selenium Webdriver 进行抓取。但是,当我在 Heroku 上部署时,Selenium 不起作用。如何让 Selenium 在 Heroku 上运行?

【问题讨论】:

  • 您好,您找到解决方案了吗?我也在服务器上使用 Node 并在前端使用 Angular,一切都在本地工作,但是在我将它部署到 heroku 上之后,selenium 不起作用
  • 在这里查看我的答案:stackoverflow.com/a/60586343/11135757

标签: node.js selenium heroku webdriver scraper


【解决方案1】:

从这里粘贴我的答案:https://stackoverflow.com/a/60586343/11135757


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

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

第 2 步:从您的终端,使用 heroku login 登录 heroku

第 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 下添加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):现在在 root-dir/ 中的 package.json 中添加此

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

第 8 步(如果您使用 react):在 root-dir/client/ 中的 package.json 中(即:package.json 用于 react 应用程序),添加以下行:

  "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 步:现在,您已准备好进行部署。确保已安装以下软件包:expressselenium-webdriverchromedriver

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

git add .
git commit -m "my app"
git push heroku master

【讨论】:

    【解决方案2】:

    下面是一个使用 selenium-webdriver npm 包和 chrome 浏览器的 javaScript 示例代码。

    const webdriver = require('selenium-webdriver');
    const chrome = require('selenium-webdriver/chrome');
    
    let options = new chrome.Options();
    //Below arguments are critical for Heroku deployment
    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');
    driver.quit();
    

    在准备好部署之前,您需要向 Heroku 添加两个构建包。

    • 使用 Heroku buildpacks 命令:
    $ heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-chromedriver
    $ heroku buildpacks:add --index 2 https://github.com/heroku/heroku-buildpack-google-chrome
    


    【讨论】:

    • 大家好,使用此设置是否可以禁用 w3c 模式?
    【解决方案3】:

    我能够使用 PhantomJs 作为无头浏览器让 Selenium Webdriver 在 Node/Heroku 上运行。我将 PhantomJs buildpack 安装到了我的 Heroku 应用程序中,它运行良好。我努力让 Chrome 和 Firefox 驱动程序在 Heroku 上运行......我写了一篇博客,其中包含我用来让它运行的步骤和代码:

    http://www.viderman.com/2017/05/selenium-on-heroku.html

    【讨论】:

    • 请在您的答案中包含步骤和代码。答案的大部分信息不应依赖于外部链接。
    • 博客文章也可以这样说 :) 外部引用有一种被删除的方式。
    猜你喜欢
    • 1970-01-01
    • 2019-04-17
    • 2015-12-23
    • 1970-01-01
    • 2014-01-22
    • 2023-03-06
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    相关资源
    最近更新 更多