在Ubuntu16上,Selenium在从Python / Celery任务运行时失败,或者从带有sudo的python运行失败。手动运行脚本运行没有问题。
示例错误:
$ sudo /var/www/my_proj/env/bin/python
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> driver = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/var/www/my_proj/env/lib/python3.5/site-packages/selenium/webdriver/chrome/webdriver.py", line 75, in __init__
desired_capabilities=desired_capabilities)
File "/var/www/my_proj/env/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/var/www/my_proj/env/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/var/www/my_proj/env/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "/var/www/my_proj/env/lib/python3.5/site-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
(Driver info: chromedriver=2.35 (0),platform=Linux 4.4.0-116-generic x86_64)
>>>
以下是没有sudo时如何完美运行:
$ /var/www/my_proj/env/bin/python
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> driver = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver')
>>>
Chromium / Xvfb安装为:
sudo apt install xvfb -y
sudo apt-get install chromium-chromedriver -y
答案 0 :(得分:3)
根据评论建议,激活日志帮助我在使用sudo
运行时查找/修复上述错误。要以超级用户身份运行,我需要--no-sandbox
参数。如下:
$ /var/www/my_proj/env/bin/python
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>>
>>> chrome_options = webdriver.ChromeOptions()
>>> chrome_options.add_argument('--no-sandbox')
>>>
>>> driver = webdriver.Chrome(
... '/usr/lib/chromium-browser/chromedriver',
... service_args=["--verbose", "--log-path=/tmp/CHROMIUM_LOG"],
... options=chrome_options)
>>>
但是当被Celery
任务调用时,我的Python代码仍然在尝试打开Chromium时出错。我已将Xvfb
作为由supervisorctl
管理的服务运行,并在DISPLAY
上设置/etc/environment
但仍然是,当Celery任务调用它时,它无效。我的解决方案是在我的Celery任务中使用pyvirtualdisplay
,如下所示:
$ pip install pyvirtualdisplay
$ /var/www/my_proj/env/bin/python
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyvirtualdisplay import Display
>>> from selenium import webdriver
>>>
>>> display = Display(visible=0, size=(1024, 768))
>>> display.start()
>>>
>>> driver = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver')
>>>
>>> driver.quit()
>>> display.stop()