简体   繁体   English

如何在ubuntu服务器后台自动运行python Selenium脚本

[英]How to auto run python Selenium script on an ubuntu server in the backgroud

What I need我需要的

I have a Python Selenium script.我有一个 Python Selenium 脚本。 When I run it on my local Ubuntu PC - it works fine But when I uploaded it to a server I face a problem.当我在本地 Ubuntu PC 上运行它时 - 它工作正常但是当我将它上传到服务器时我遇到了问题。 The server has no display I solved this problem with X Virtual Framebuffer display.服务器没有显示器 我用 X Virtual Framebuffer 显示器解决了这个问题。 What I need - is to automatically setup the display and run my script in the background我需要的是自动设置显示并在后台运行我的脚本

Problem问题

Now I run it manually the following way现在我通过以下方式手动运行它

  1. I go to the terminal我 go 到终端

  2. Set the display with the following commands使用以下命令设置显示

    export DISPLAY=:1导出显示=:1

    Xvfb $DISPLAY -screen $DISPLAY 1280x1024x16 & Xvfb $DISPLAY -screen $DISPLAY 1280x1024x16 &

  3. Run the python script with command python3 products2.py使用命令 python3 products2.py 运行 python 脚本

This works fine.这很好用。

But I need it to run automatically in the background I created a conf file for supervisor and run the python script with supervisor.但我需要它在后台自动运行我为主管创建了一个 conf 文件并与主管一起运行 python 脚本。

[program:prod]
command = /root/lowescom/l-env/bin/python3.10 /root/lowescom/lowes_project/modules/products2.py
user = root
autorestart = true
redirect_stderr = true
stdout_logfile = /root/lowescom/lowes_project/logs/debug.log

But this don't work.但这不起作用。 Even if I set up the display manually - it doesn't work即使我手动设置显示 - 它也不起作用

Question问题

How can I run my python Selenium script in the background automatically.如何在后台自动运行我的 python Selenium 脚本。 The display setup should also be automated.显示设置也应该是自动化的。

Update更新

I have just tried to use no-sandbox.我刚刚尝试使用无沙盒。 But still not working但仍然没有工作

chrome_options = uc.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

driver = uc.Chrome(use_subprocess=True, options=chrome_options)  

if you are using chromedriver you should set the option:如果你使用的是 chromedriver,你应该设置选项:

chrome_options.add_argument('--no-sandbox')

for firefox you can check the pyvirtualdisplay module.对于 firefox,您可以检查 pyvirtualdisplay 模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM