簡體   English   中英

在 GitHub 中運行 Selenium Webdriver 用於單元測試的操作

[英]Running Selenium Webdriver in GitHub Actions for unit tests

I'm trying to run a unit test in GitHub actions before deploying my AWS Lambda , where I'm running Selenium Webdriver with a functioning Selenium-chromium lambda layer ( https://github.com/vittorio-nardone/selenium-chromium- λ )。 我在Python 3.6中運行我的環境並使用 ChromeDriver 版本 95.0.4638.54

我在運行單元測試時一直遇到這個錯誤:

selenium.common.exceptions.WebDriverException: 
Message: unknown error: Chrome failed to start: exited abnormally. 
(chrome not reachable)

我使用了與運行 AWS Lambda 時相同的 Chrome 選項(有效):

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 特定的一些附加選項:

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"

最后,我正在啟動驅動程序:

driver = webdriver.Chrome(options=chrome_options)

這在 Lambda 中運行良好,但每次我嘗試在 GitHub 操作中運行單元測試時都會失敗。

這是在 GitHub 操作中運行作業時的配置:

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

配置完全取自Selenium 自己進行的 GibHub 操作中的Chrome 設置https://github.com/SeleniumHQ/selenium/blob/selenium-4.0.0-beta-3/.github/actions/setup-chrome/ action.yml ),效果很好。

這個 GitHub 操作配置使用的是最新的 ChromeDriver,所以我都嘗試過舊版本(如前所述的 95.0.4638.54)和最新版本(96.0.4664.45),但我仍然遇到相同的錯誤:

selenium.common.exceptions.WebDriverException: 
Message: unknown error: Chrome failed to start: exited abnormally. 
(chrome not reachable)

這是我需要添加到 Chrome 選項以使其工作的內容。

首先添加一個遠程調試端口

chrome_options.add_argument('--remote-debugging-port=9222')

然后我還將二進制位置更改為以下位置, binary_location選項適用於谷歌瀏覽器:

chrome_options.binary_location = "/usr/bin/google-chrome"

最后,正如這個答案中提到的( https://stackoverflow.com/a/60092331/6697714 ),我還向 Chrome 驅動程序添加了一個可執行路徑

executable_path="/usr/local/bin/chromedriver"

lambda 配置保持不變:

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)

以下是最終配置的樣子:

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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM