linux google chrome在EC2示例中无法启动

pn9klfpd  于 2023-01-29  发布在  Linux
关注(0)|答案(1)|浏览(137)

我想使用google-chrome在EC2示例中运行一个简单的python代码,google-chrome在我的本地系统中运行良好虽然我已经安装了所有必需的软件包,但我无法运行脚本Google Chrome 109. 0. 5414. 119 ChromeDriver 109. 0. 5414. 74 selenium 4. 8. 0
当运行下面的简单脚本时

#######################

from selenium import webdriver

service = webdriver.chrome.service.Service('/usr/bin/chromedriver')
service.start()
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options = options.to_capabilities()
driver = webdriver.Remote(service.service_url, options)

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

###############################

我收到以下错误

######################### raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed. (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) Stacktrace: \#0 0x55cd3bfbd303 \<unknown\> \#1 0x55cd3bd91d37 \<unknown\> \#2 0x55cd3bdba157 \<unknown\> \#3 0x55cd3bdb6330 \<unknown\> \#4 0x55cd3bdf74a6 \<unknown\> \#5 0x55cd3bdee753 \<unknown\> \#6 0x55cd3bdc1a14 \<unknown\> \#7 0x55cd3bdc2b7e \<unknown\> \#8 0x55cd3c00c32e \<unknown\> \#9 0x55cd3c00fc0e \<unknown\> \#10 0x55cd3bff2610 \<unknown\> \#11 0x55cd3c010c23 \<unknown\> \#12 0x55cd3bfe4545 \<unknown\> \#13 0x55cd3c0316a8 \<unknown\> \#14 0x55cd3c031836 \<unknown\> \#15 0x55cd3c04cd13 \<unknown\> \#16 0x7f0f6b23e44b start_thread

wmtdaxz3

wmtdaxz31#

我假设你正在运行一个Linux EC2示例,下面是在Ubuntu Linux EC2上运行代码的说明,其他操作系统会略有不同。
连接到EC2示例并运行:

sudo snap install chromium
wget https://chromedriver.storage.googleapis.com/109.0.5414.74/chromedriver_linux64.zip
sudo apt update
sudo apt install unzip -y
unzip chromedriver_linux64.zip 
sudo apt install python3-pip -y
pip3 install selenium --user

或者将这些命令添加到EC2用户数据中:

#!/bin/bash
cd /home/ubuntu
snap install chromium
wget https://chromedriver.storage.googleapis.com/109.0.5414.74/chromedriver_linux64.zip
apt update
apt install unzip -y
unzip chromedriver_linux64.zip 
apt install python3-pip -y
sudo -u ubuntu pip3 install selenium  --user

然后将脚本更新为:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=/home/ubuntu/snap/chromium/common/chromium/Default")
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)
driver.get("https://www.google.com")
print(driver.page_source)
driver.quit()

然后使用python3运行脚本,例如,如果文件名为test.py,则运行python3 test.py
它将打印所请求网页的源代码,即https://www.google.com

相关问题