首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在单元测试的GitHub操作中运行Selenium

在单元测试的GitHub操作中运行Selenium
EN

Stack Overflow用户
提问于 2021-12-13 23:08:15
回答 1查看 1.9K关注 0票数 2

在部署我的AWS Lambda之前,我正在尝试在GitHub actions中运行一个单元测试,在这里,我使用一个功能良好的Selenium- in层(https://github.com/vittorio-nardone/selenium-chromium-lambda)运行Selenium Webdriver。我正在使用Python3.6运行我的环境,并使用ChromeDriver版本95.0.4638.54

在运行单元测试时,我一直遇到这个错误:

代码语言:javascript
运行
复制
selenium.common.exceptions.WebDriverException: 
Message: unknown error: Chrome failed to start: exited abnormally. 
(chrome not reachable)

在运行AWS Lambda时,我使用了与运行AWS Lambda相同的Chrome选项:

代码语言:javascript
运行
复制
lambda_options = [
    '--autoplay-policy=user-gesture-required',
    '--disable-background-networking',
    '--disable-background-timer-throttling',
    '--disable-backgrounding-occluded-windows',
    '--disable-breakpad',
    '--disable-client-side-phishing-detection',
    '--disable-component-update',
    '--disable-default-apps',
    '--disable-dev-shm-usage',
    '--disable-domain-reliability',
    '--disable-extensions',
    '--disable-features=AudioServiceOutOfProcess',
    '--disable-hang-monitor',
    '--disable-ipc-flooding-protection',
    '--disable-notifications',
    '--disable-offer-store-unmasked-wallet-cards',
    '--disable-popup-blocking',
    '--disable-print-preview',
    '--disable-prompt-on-repost',
    '--disable-renderer-backgrounding',
    '--disable-setuid-sandbox',
    '--disable-speech-api',
    '--disable-sync',
    '--disk-cache-size=33554432',
    '--hide-scrollbars',
    '--ignore-gpu-blacklist',
    '--ignore-certificate-errors',
    '--metrics-recording-only',
    '--mute-audio',
    '--no-default-browser-check',
    '--no-first-run',
    '--no-pings',
    '--no-sandbox',
    '--no-zygote',
    '--password-store=basic',
    '--use-gl=swiftshader',
    '--use-mock-keychain',
    '--single-process',
    '--headless']

for argument in lambda_options:
    chrome_options.add_argument(argument)

有一些特定于Lambda的其他选项:

代码语言:javascript
运行
复制
chrome_options.add_argument('--user-data-dir={}'.format(tmp_folder + '/user-data'))
chrome_options.add_argument('--data-path={}'.format(tmp_folder + '/data-path'))
chrome_options.add_argument('--homedir={}'.format(tmp_folder))
chrome_options.add_argument('--disk-cache-dir={}'.format(tmp_folder + '/cache-dir'))

chrome_options.binary_location = "/opt/bin/chromium"

tmp_folder是AWS提供的文件系统,其中tmp_folder = /tmp

最后,我要启动司机:

代码语言:javascript
运行
复制
driver = webdriver.Chrome(options=chrome_options)

这在Lambda中非常好,但是每次我尝试在GitHub操作中运行单元测试时都会失败。

这是在GitHub操作中运行作业时的配置:

代码语言:javascript
运行
复制
jobs:
  build:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Set up Python 3.7
        uses: actions/setup-python@v2
        with:
          python-version: 3.7
      - name: Install dependencies
        run: |
        .......

      - name: Unit test
        run: |

          wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
          echo "deb http://dl.google.com/linux/chrome/deb/ stable main" | sudo tee -a /etc/apt/sources.list.d/google-chrome.list
          sudo apt-get update -qqy
          sudo apt-get -qqy install google-chrome-stable
          CHROME_VERSION=$(google-chrome-stable --version)
          CHROME_FULL_VERSION=${CHROME_VERSION%%.*}
          CHROME_MAJOR_VERSION=${CHROME_FULL_VERSION//[!0-9]}
          sudo rm /etc/apt/sources.list.d/google-chrome.list
          export CHROMEDRIVER_VERSION=`curl -s https://chromedriver.storage.googleapis.com/LATEST_RELEASE_${CHROME_MAJOR_VERSION%%.*}`
          curl -L -O "https://chromedriver.storage.googleapis.com/${CHROMEDRIVER_VERSION}/chromedriver_linux64.zip"
          unzip chromedriver_linux64.zip && chmod +x chromedriver && sudo mv chromedriver /usr/local/bin
          export CHROMEDRIVER_VERSION=`curl -s https://chromedriver.storage.googleapis.com/LATEST_RELEASE_${CHROME_MAJOR_VERSION%%.*}`
          curl -L -O "https://chromedriver.storage.googleapis.com/${CHROMEDRIVER_VERSION}/chromedriver_linux64.zip"
          unzip chromedriver_linux64.zip && chmod +x chromedriver && sudo mv chromedriver /usr/local/bin
          chromedriver -version
          which chromedriver
          which google-chrome

该配置完全取自Chrome安装程序,由Selenium自身 (https://github.com/SeleniumHQ/selenium/blob/selenium-4.0.0-beta-3/.github/actions/setup-chrome/action.yml)制作的GibHub操作完成,工作非常出色。

这个GitHub操作配置使用的是最新的ChromeDriver,因此我尝试了以前的版本(95.0.4638.54如前所述)和最新的版本(96.0.4664.45),但我仍然得到相同的错误:

代码语言:javascript
运行
复制
selenium.common.exceptions.WebDriverException: 
Message: unknown error: Chrome failed to start: exited abnormally. 
(chrome not reachable)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-13 23:08:15

这里是我需要添加到Chrome选项,使其工作。

首先添加远程调试端口

代码语言:javascript
运行
复制
chrome_options.add_argument('--remote-debugging-port=9222')

然后,我还将二进制位置更改为以下位置,binary_location选项用于Google:

代码语言:javascript
运行
复制
chrome_options.binary_location = "/usr/bin/google-chrome"

最后,正如在这个答案(https://stackoverflow.com/a/60092331/6697714)中提到的,我还在Chrome驱动程序中添加了一个可执行路径

代码语言:javascript
运行
复制
executable_path="/usr/local/bin/chromedriver"

照常保留lambda配置:

代码语言:javascript
运行
复制
lambda_options = [
    '--autoplay-policy=user-gesture-required',
    '--disable-background-networking',
    '--disable-background-timer-throttling',
    '--disable-backgrounding-occluded-windows',
    '--disable-breakpad',
    '--disable-client-side-phishing-detection',
    '--disable-component-update',
    '--disable-default-apps',
    '--disable-dev-shm-usage',
    '--disable-domain-reliability',
    '--disable-extensions',
    '--disable-features=AudioServiceOutOfProcess',
    '--disable-hang-monitor',
    '--disable-ipc-flooding-protection',
    '--disable-notifications',
    '--disable-offer-store-unmasked-wallet-cards',
    '--disable-popup-blocking',
    '--disable-print-preview',
    '--disable-prompt-on-repost',
    '--disable-renderer-backgrounding',
    '--disable-setuid-sandbox',
    '--disable-speech-api',
    '--disable-sync',
    '--disk-cache-size=33554432',
    '--hide-scrollbars',
    '--ignore-gpu-blacklist',
    '--ignore-certificate-errors',
    '--metrics-recording-only',
    '--mute-audio',
    '--no-default-browser-check',
    '--no-first-run',
    '--no-pings',
    '--no-sandbox',
    '--no-zygote',
    '--password-store=basic',
    '--use-gl=swiftshader',
    '--use-mock-keychain',
    '--single-process',
    '--headless',
]

for argument in lambda_options:
    chrome_options.add_argument(argument)

最后的配置如下所示:

代码语言:javascript
运行
复制
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--user-data-dir={}'.format(tmp_folder + '/user-data'))
chrome_options.add_argument('--data-path={}'.format(tmp_folder + '/data-path'))
chrome_options.add_argument('--homedir={}'.format(tmp_folder))
chrome_options.add_argument('--disk-cache-dir={}'.format(tmp_folder + '/cache-dir'))
chrome_options.add_argument('--remote-debugging-port=9222')

chrome_options.binary_location = "/usr/bin/google-chrome"
driver = webdriver.Chrome(options=chrome_options, executable_path="/usr/local/bin/chromedriver")
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70341964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档