在部署我的AWS Lambda之前,我試圖在GitHub 操作中運行單元測驗,我在其中運行Selenium Webdriver和一個功能正常的 Selenium-chromium lambda 層(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)
uj5u.com熱心網友回復:
這是我需要添加到 Chrome 選項以使其作業的內容。
首先添加一個遠程除錯埠:
chrome_options.add_argument('--remote-debugging-port=9222')
然后我也將二進制位置更改為以下位置,該binary_location選項適用于 Google Chrome:
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")
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/392818.html
