TLDR;
Python Selenium using ChromeDriver installed on EC2 instance not working when the script is called from the browser but working when same script called from the server. How to fix?
Hey guys,
I have installed Python 3.6, Selenium, ChromeDriver, and Google Chrome on my AWS EC2 instance. I have also enabled HTTP/2 and given ChromeDriver permissions.
I have a test Python script, let's call it test.py.
#!/usr/bin/env pythonfrom selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsoptions = Options()options.headless = Truedriver = webdriver.Chrome('/usr/bin/chromedriver', options=options)driver.get("https://www.youtube.com/")element_text = driver.find_element_by_id("title").textprint(element_text)
In the terminal, I can simply run this script with:
python3 /var/app/current/scripts/test.py
and it works with no problem.
However, if I try to run this same script from the browser, (note that the URL is HTTPS), I get the following error:
[mpm_event:notice] AH00493: SIGUSR1 received. Doing graceful restart[http2:warn] AH02951: mod_ssl does not seem to be enabled[lbmethod_heartbeat:notice] AH02282: No slotmem from mod_heartmonitor[mpm_event:notice] AH00489: Apache/2.4.41 (Amazon) configured -- resuming normal operations[core:notice] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'[mpm_event:warn] AH00488: long lost child came home!Traceback (most recent call last): File "/var/app/current/scripts/test.py", line 3, in <module> from selenium import webdriverModuleNotFoundError: No module named 'selenium'
stating that there is no module named 'selenium'.
Note that I am calling it from the browser using PHP:
$output=shell_exec('python3 /var/app/current/scripts/test.py');echo "<pre>$output</pre>";
I also know that the PHP is working because if I change test.py to simply:
import datetime from datetimeprint(datetime.now())
It will print the date and time.
So, there seems to be a problem with Selenium in particular, but only from the browser.
Does anyone have any idea what this could be?
I appreciate all of your help / comments!
Thanks a lot :)
Brad
EDIT
I have since changed the script to:
sudo PYTHONUSERBASE=/home/ec2-user/.local python3 /var/app/current/scripts/test.py
and am now getting a slightly different error:
sudo: no tty present and no askpass program specified
I think I'll have to create a virtualenvironment and then use that when I call the script again.