I want to run a simple python code in EC2 instance using google-chrome which works fine in my local system Though I have installed all the required packages I am not able to run the script Google Chrome 109.0.5414.119 ChromeDriver 109.0.5414.74 selenium 4.8.0
while running a below simple script
#######################
```
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")
```
###############################
I am getting the following error
#########################
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
CodePudding user response:
The error message suggests that the Chrome browser is not running correctly on the EC2 instance, which is causing the ChromeDriver to assume that Chrome has crashed. This is likely caused by an issue with the version of Chrome and ChromeDriver that you are using. Try updating both to the latest version and see if that resolves the issue. Also, please check whether the chrome browser is installed in the ec2 instance or not. Also, make sure that the chrome browser installed in the EC2 instance is the same version as the chrome driver.
CodePudding user response:
I'm assuming you are running a Linux EC2 instance. Here are the instructions for running your code on a Ubuntu Linux EC2 other OSs will be slightly different.
Either connect to you EC2 instance and run:
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
Or add these commands to your EC2 user data and cd into your ubuntu user home directory first i.e. cd /home/ubuntu
and su - ubuntu
before running pip3 install selenium --user
Then update your script to be:
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()
Then run your script with python3 e.g. python3 test.py
if your file is called test.py
.
It will print the source of the web page requested i.e. https://www.google.com