首页 > 解决方案 > Selenium Webdriver Chrome 在云功能上失败

问题描述

我正在尝试在 Cloud Function 上使用 selenium webdriver chrome 运行一个简单的代码。我收到以下错误

消息:“chromedriver”可执行文件需要在 PATH 中。请参阅https://sites.google.com/a/chromium.org/chromedriver/home

如何在 Cloud Function 中指定 chromedriver 可执行文件?

下面是我的代码

from selenium import webdriver
import time
def test_webdriver(event=None, context=None):
    driver = webdriver.Chrome()
    driver.get('http://www.google.com/');
    time.sleep(5)
    search_box = driver.find_element_by_name('q')

标签: pythongoogle-cloud-platformgoogle-cloud-functionsselenium-chromedriver

解决方案


Cloud Functions 运行时中当前不提供 Headless Chrome。

如果您有能力为您的 Cloud Function 切换到 Node.js 运行时,您可以使用puppeteer其中包括无头 Chrome,但 Python 没有等价物。

另一种选择是使用 Cloud Run 而不是 Cloud Functions。见这里的例子:https ://dev.to/di/using-headless-chrome-with-cloud-run-3fdp


推荐阅读