首页 > 解决方案 > 无法让 selenium webdriver 在 azure databricks 中工作

问题描述

我们有一些 python 脚本可以抓取网站并且运行良好。现在我们想在 Azure Databricks 中执行此操作。我们认为通过 Databricks 论坛中的以下帖子可以解决此问题,但不幸的是,它不起作用。(https://forums.databricks.com/questions/15480/how-to-add-webdriver-for-selenium-in-databricks.html?childToView=21347#answer-21347

运行最后一段代码后我们得到的错误是:WebDriverException: Message: unknown error: cannot find Chrome binary (Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Linux 4.15.0-1050-azure x86_64)

最后一段代码如下所示:

    %py

    from selenium import webdriver

    chrome_options = webdriver.ChromeOptions()

    chrome_options.add_argument('--no-sandbox')

    chrome_options.add_argument('--headless')

    chrome_options.add_argument('--disable-dev-shm-usage')

    chrome_driver = "/tmp/chromedriver/chromedriver"

    driver = webdriver.Chrome(chrome_driver, 
    chrome_options=chrome_options)

    driver.get("https://www.google.com")

我发现了一个帖子,我必须在其中给出二进制文件的位置: Selenium 在 Mac 上给出“selenium.common.exceptions.WebDriverException:消息:未知错误:找不到 Chrome 二进制文件”

    options.binary_location = "/Applications/Google 
    Chrome.app/Contents/MacOS/Google Chrome"

但我不知道此二进制文件在 Azure Databricks 中的文件位置。

标签: binaryselenium-chromedriverazure-databricks

解决方案


好吧,在对原始 scipt 进行小改动后,我已经让它工作了

    %sh /databricks/python3/bin/pip3 install selenium
    ==================
    %sh
    wget      
    https://chromedriver.storage.googleapis.com/73.0.3683.68/chromedriver_linux64.zip 
    -O /tmp/chromedriver_linux64.zip
    ==================
    %sh mkdir /tmp/chromedriver
    ================
    %sh
    unzip /tmp/chromedriver_linux64.zip -d /tmp/chromedriver/
    ==================
    %sh
    sudo add-apt-repository ppa:canonical-chromium-builds/stage
    ===================
    %sh
    /usr/bin/yes | sudo apt update
    ===================
    %sh
    /usr/bin/yes | sudo apt install chromium-browser
    ===================
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_driver = "/tmp/chromedriver/chromedriver"
    driver = webdriver.Chrome(chrome_driver,chrome_options=chrome_options)
    driver.get("https://www.google.com")

该脚本下载并将 chromium 更新到版本 77。虽然 chromedriver 是 73。更改 rhe 链接以下载 chromedriver 77 就可以了。

    wget 
    https://chromedriver.storage.googleapis.com/77.0.3865.40/chromedriver_linux64.zip 

推荐阅读