0

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.

Kelvin Low
  • 678
  • 1
  • 10
  • 23

1 Answers1

0

The problem isn't with running chrome in a headless environment, the problem is with running tkinter. Tkinter requires a display, either a physical display or the case of a linux environment an X11 display. For the most part, that means tkinter can't run in a server environment unless you set up that environment to have a virtual display for the tkinter process (eg: xvfb).

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thanks for the answer. Just like I suspected. But I did specify `--headless` on Mac so why is it able to run? I also installed xvfb by following this answer https://stackoverflow.com/a/61043049/5685946 but it didn't work either. I'll edit my question to include xvfb. – Kelvin Low Dec 14 '20 at 16:47
  • 1
    @KelvinLow: tkinter has no "--headless" option. – Bryan Oakley Dec 14 '20 at 16:56