I am trying to get Selenium working on EC2 machines and am running into an issue that disappears when VS Code connects through an SSH connection. If I connect using a terminal only, the issue persists.
Some context
I’ve got Selenium working through a simple Python script and am running into an issue where I want this automated on some EC2 machines. Here’s an example script to show the behavior:
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
if __name__ == '__main__':
chrome_options = Options()
chrome_options.add_argument('--headless')
service = Service('/usr/bin/chromedriver')
driver = Chrome(service=service, options=chrome_options)
driver.get("https://example.com/")
print(driver.title)
driver.quit()
When running this on a machine that never had VS Code connected to it, it will exit with the following error:
Traceback (most recent call last):
File "/usr/share/stratoverse/SCC/SCC/appScripts/selenium/proofOfWork.py", line 17, in <module>
driver = Chrome(service=service, options=chrome_options)
File "/home/ubuntu/.local/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
super().__init__(
File "/home/ubuntu/.local/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 55, in __init__
self.service.start()
File "/home/ubuntu/.local/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 102, in start
self.assert_process_still_running()
File "/home/ubuntu/.local/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 115, in assert_process_still_running
raise WebDriverException(f"Service {self._path} unexpectedly exited. Status code was: {return_code}")
selenium.common.exceptions.WebDriverException: Message: Service /usr/bin/chromedriver unexpectedly exited. Status code was: 1
Now comes the interesting part. If you make an SSH connection with VS Code, all subsequent runs will work correctly until the machine is terminated. Without changing any (environment) variables or scripts. This happens on Ubuntu 22.04, I thought that it was caused by the /home/ubuntu
folder being encrypted at first, but that was not the case. Installing Selenium in a different location doesn’t resolve this problem either, the environments are identical when executing these runs
My question
So my question is: What does VS Code do upon connecting that doesn’t happen when a terminal connects to it? (and how can I trick the WebDriver to work without connecting to it first, the goal is to use a crontab)