【问题标题】:Selenium Python/Django: using it with chromedriver on ubuntuSelenium Python/Django:在 ubuntu 上与 chromedriver 一起使用
【发布时间】:2018-06-06 08:16:13
【问题描述】:

我在 Django 应用程序聊天中有以下代码

tests.py

from channels.testing import ChannelsLiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait

class ChatTests(ChannelsLiveServerTestCase):
    serve_static = True  # emulate StaticLiveServerTestCase

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        try:
            # NOTE: Requires "chromedriver" binary to be installed in $PATH /home/muiruri_samuel/webapp/chatenv/lib/python3.6/site-packages/
            cls.driver = webdriver.Chrome(executable_path="/home/muiruri_samuel/webapp/chatsys/chat/chromedriver")
        except:
            super().tearDownClass()
            raise

    ...

它应该检查!如果通道工作正常,但在此之前无法连接到 ChromeDriver 并出现此错误

(chatenv) muiruri_samuel@train:~/webapp/chatsys$ python manage.py test chat.tests
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
E
======================================================================
ERROR: setUpClass (chat.tests.ChatTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/muiruri_samuel/webapp/chatenv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", l
ine 76, in start
    stdin=PIPE)
  File "/usr/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/home/muiruri_samuel/webapp/chatsys/chat/chromedriver':
 '/home/muiruri_samuel/webapp/chatsys/chat/chromedriver'
 line 68, in __init__
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/muiruri_samuel/webapp/chatsys/chat/tests.py", line 14, in setUpClass
    cls.driver = webdriver.Chrome(executable_path="/home/muiruri_samuel/webapp/chatsys/chat/chromedriver")
  File "/home/muiruri_samuel/webapp/chatenv/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 68, in __init__
    self.service.start()
  File "/home/muiruri_samuel/webapp/chatenv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home


----------------------------------------------------------------------
Ran 0 tests in 0.006s

FAILED (errors=1)
Destroying test database for alias 'default'...

它“没有”在 PATH /home/muiruri_samuel/webapp/chatsys/chat/chromedriver 上找到驱动程序,我也尝试将其复制到站点包 /home/muiruri_samuel/webapp/chatenv/lib/python3.6/site-packages/

【问题讨论】:

  • 例如复制到/usr/bin

标签: python django selenium


【解决方案1】:

根据muiruri_samuel@train:~/webapp/chatsys$ python manage.py test chat.tests 命令,您正在运行文件夹webapp/chatsys 中的代码。 所以它将是一个工作目录。因此它不会在子目录中找到chromedriver,包括./chat。 例如,尝试将chromedriver 移动到/usr/bin。 并确保它在PATH 变量中:echo $PATH

【讨论】:

  • 使用 sudo 复制它,重新启动 gunicorn 并再次尝试,但仍然出现相同的错误。我还检查了路径是否在路径(chatenv) muiruri_samuel@train:/usr/bin$ echo $PATH /home/muiruri_samuel/webapp/chatenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games: /usr/local/games:/snap/bin
  • /usr/bin 在路径文件夹中
  • 如果在chat/tests.py 中添加import os; print(os.environ['PATH']) 会得到什么?
  • /home/muiruri_samuel/webapp/chatenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/ usr/games: /usr/local/games:/snap/bin
  • 打印上面的行。
猜你喜欢
  • 2014-04-23
  • 2016-05-01
  • 2012-03-21
  • 2023-03-24
  • 1970-01-01
  • 2018-11-25
  • 2021-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多