skip to Main Content

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


  1. You need to provide the DISPLAY env.

    Try this:

    01 * * * * DISPLAY=":0" python3 /home/someuser/scripts/FBUpload.py
    

    If this didn’t work try to find the right value by checking the current env:

    echo $DISPLAY
    
    Login or Signup to reply.
  2. 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:

    Pyperclip could not find a copy/paste mechanism for your system
    

    Solution

    Check this command at the shell:

    xclip
    

    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:

    echo $DISPLAY
    

    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:

    $ sudo apt-get install xvfb python-pip
    $ sudo pip install pyvirtualdisplay
    

    In your program you can:

    #!/usr/bin/env python
    
    from pyvirtualdisplay import Display
    from selenium import webdriver
    
    display = Display(visible=0, size=(800, 600))
    display.start()
    
    # now Browser will run in a virtual display but won't be visible
    driver = webdriver.Firefox()
    driver.get('http://www.google.com')
    print driver.title
    browser.quit()
    
    display.stop()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search