本文介绍了如何在Heroku上使用Selenium Webdriver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发Node.js应用程序,并在其上使用Selenium Webdriver进行抓取.但是,当我在Heroku上部署时,Selenium无法正常工作.如何让Selenium在Heroku上工作?
I am developing a Node.js app, and I use Selenium Webdriver on it for scraping purposes. However, when I deploy on Heroku, Selenium doesn't work. How can I make Selenium work on Heroku?
推荐答案
下面是使用selenium-webdriver npm包和chrome浏览器的javaScript示例代码.
Below is a javaScript sample code using selenium-webdriver npm package with chrome browser.
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中添加两个buildpack.
Before you are ready to deploy, you would need to add two buildpacks to 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
或
- 在Heroku仪表板中进行配置:
设置->添加构建包-> https://github.com/heroku/heroku-buildpack-chromedriver ->保存更改
设置->添加构建包-> https://github.com/heroku/heroku-buildpack -google-chrome ->保存更改
- Config in Heroku dashboard:
Settings -> Add buildpacks -> https://github.com/heroku/heroku-buildpack-chromedriver -> Save changes
Settings -> Add buildpacks -> https://github.com/heroku/heroku-buildpack-google-chrome -> Save changes
这篇关于如何在Heroku上使用Selenium Webdriver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!