在Cron中运行无头硒时,出现错误的“ PyperClip找不到系统的复制/粘贴机制”
我已经在Python中实现了Selenium脚本,以将一些图片和内容上传到Facebook,我将其命名为fbupload.py。
当我以这种方式启动它时,它可以完美地工作(在无头模式下):
Xvfb :10 -ac &
python3 /home/someuser/scripts/FBUpload.py
问题是,当我尝试配置启动相同脚本的cronjob时,这种方式是这样:
00 * * * * Xvfb :10 -ac &
01 * * * * python3 /home/someuser/scripts/FBUpload.py
45 * * * * kill -9 $(ps -auxw |grep Xvf|head -1| awk '{print $2}')
然后,它会失败,而以下错误:
pyperclip找不到系统的复制/粘贴机制
这是我的设置: Ubuntu 20.04.4 LTS | python3 | PyperClip 1.7.0
这些是副本&我已经安装的糊剂机制:(
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)
我无法按照本文所述下载pyqt4或qkt: pyperclip模块提出了一条错误消息所以我遵循建议的解决方案下载了qtpy。但是问题坚持不懈。)
我尝试了帖子中有类似问题的修复程序,但它们都不适合我。我想知道问题是否与用户有关(因为当我使用“ sudo”运行脚本时,root用户找不到非root用户安装的库)。
我还发现了一个似乎相似的另一个问题(但是,问题是Systemd): ubuntu 16.04- python 3-终端和通过Systemd
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
剪贴板是GUI不可或缺的一部分。由于您正在使用
xvfb
大概您的系统没有自己的GUI。因此,没有剪贴板可以复制和粘贴。 pyperclip 可以访问,因此,无论您尝试哪种方式要访问它,您将面临错误:解决方案
在Shell上检查此命令:
如果输出为
错误:无显示: < null>
事情可能会变得更加困难。但是,为了拥有GUI,因为您可以通过SSH设置A tunnel X11到台式机上的X服务器。如果您仍然看到XCLIP的错误,则设置的问题是。最简单的是检查:
如果您获得空白输出,那么您的会话对X11隧道一无所知。您需要正确设置隧道。设置正确修复后,PyperClip和您的程序应正常工作。
替代方法
另一种方法是使用 pyvirtualdisplay 运行无头硒网络驱动器测试。
在Ubuntu/Debian上安装PyvirtualDisplay:
在您的程序中,您可以:
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:
您需要提供
显示
env。尝试以下操作:
如果这不起作用,请通过检查当前的env来找到正确的值:
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: