I'm writing a Python script that opens a link and click on a link to copy content into the clipboard. Then I use Tkinter to pass the content into a variable. Below is part of my script:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument("--headless")
chromeOptions.add_argument('window-size=1366x768')
driver = webdriver.Chrome(options=chromeOptions)
driver.get('https://www.example.com')
r = Tk()
r.withdraw()
while not r.selection_get(selection="CLIPBOARD"):
time.sleep(0.1)
copyResult = r.selection_get(selection="CLIPBOARD")
When executing the script on Mac it works flawlessly but on Ubuntu I get this error:
Traceback (most recent call last):
File "DeliveryTracking.py", line 52, in <module>
r = Tk()
File "/usr/lib/python3.8/tkinter/__init__.py", line 2261, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
My Ubuntu server is hosted on Amazon EC2 so there's no GUI. Is that why I'm getting this error on Ubuntu? If yes then what's an alternative? Anyway on Mac I also specified --headless
argument so by right it shouldn't use any GUI to run the script. I may be wrong though.
Edit:
I've tried installing xvfb by following this link. However I'm still getting the following error when executing my script:
Traceback (most recent call last):
File "DeliveryTracking.py", line 49, in <module>
driver = webdriver.Chrome(options=chromeOptions)
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
(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.)
And if I just run Google Chrome I get this error [15352:15352:1215/003611.154158:ERROR:browser_main_loop.cc(1426)] Unable to open X display.