I have implemented a selenium script in Python to upload some pictures and content to Facebook, which I named FBUpload.py.
When I launch it this way, it works perfectly (in headless mode):
Xvfb :10 -ac &
python3 /home/someuser/scripts/FBUpload.py
Problem is, when I try to configure a cronjob that launches this same script, this way:
00 * * * * Xvfb :10 -ac &
01 * * * * python3 /home/someuser/scripts/FBUpload.py
45 * * * * kill -9 $(ps -auxw |grep Xvf|head -1| awk '{print $2}')
Then it fails with the following error:
Pyperclip could not find a copy/paste mechanism for your system
This is my setup:
Ubuntu 20.04.4 LTS |
Python3 |
pyperclip 1.7.0
These are the Copy & paste mechanisms that I already installed:
PyQt5 5.15.6
PyQt5-Qt5 5.15.2
PyQt5-sip 12.10.1
QtPy 2.1.0
xclip 0.13-1 (in /usr/bin because it was installed via apt)
xsel 1.2.0+git9bfc13d.20180109-3 (in /usr/bin because it was installed via apt)
(I couldn’t download PyQt4 or qkt as described in this post: pyperclip module raising an error message so I downloaded QtPy following the suggested solution. But the problem persists.)
I tried the fixes from posts with similar issue but none of them work for me. I am wondering if the issue has to do with users (because when I run the script with "sudo", the root user cannot find the libraries installed by the non-root user).
I also found this other question which seems to be similar (but instead of cron, the problem is systemd): Ubuntu 16.04 – Python 3 – Pyperclip in terminal and via systemd
2
Answers
You need to provide the
DISPLAY
env.Try this:
If this didn’t work try to find the right value by checking the current env:
The clipboard is an integral part of the GUI. Since you are using
Xvfb
presumably your system doesn’t have it’s own GUI. So there is no clipboard to copy and paste. There is no clipboard for pyperclip to access, so whichever way you try to access it you will face the error:Solution
Check this command at the shell:
If the output is
Error: No display: <null>
things may get more difficult.However, in order to have a GUI, because you can setup a tunnel X11 through ssh to an X server on your desktop machine. If you still see an error from xclip, then the problem is with your setup. The simplest thing would be to check:
If you get a blank output then your session doesn’t know anything about your X11 tunnel. You need to setup tunneling properly. Once your setup is properly fixed, pyperclip and your program should be working.
Alternative approach
An alternative approach would be to use PyVirtualDisplay (a Python wrapper for Xvfb, Xephyr and Xvnc) to run headless Selenium WebDriver tests.
Install PyVirtualDisplay on Ubuntu/Debian:
In your program you can: