Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 98111

Importing opencv crashes selenium

$
0
0

If I add import cv2 statement, selenium Chrome browser fails to start with some error which doesn't even seem to correlate with opencv.

Sanity checks passed. Maybe I miss something obvious here?

Full error traceback:

Traceback (most recent call last):  File "/home/nikolay/*redacted*/2.py", line 4, in <module>    browser = webdriver.Chrome()              ^^^^^^^^^^^^^^^^^^  File "/home/nikolay/.local/lib/python3.11/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__    super().__init__(  File "/home/nikolay/.local/lib/python3.11/site-packages/selenium/webdriver/chromium/webdriver.py", line 61, in __init__    super().__init__(command_executor=executor, options=options)  File "/home/nikolay/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 209, in __init__    self.start_session(capabilities)  File "/home/nikolay/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 293, in start_session    response = self.execute(Command.NEW_SESSION, caps)["value"]               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "/home/nikolay/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 348, in execute    self.error_handler.check_response(response)  File "/home/nikolay/.local/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response    raise exception_class(message, screen, stacktrace)selenium.common.exceptions.SessionNotCreatedException: Message: session not created: Chrome failed to start: exited normally.  (session not created: DevToolsActivePort file doesn't exist)  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)Stacktrace:#0 0x55a0d0470d93 <unknown>#1 0x55a0d0154337 <unknown>#2 0x55a0d0188bc0 <unknown>#3 0x55a0d0184765 <unknown>#4 0x55a0d01ceb7c <unknown>#5 0x55a0d01c21e3 <unknown>#6 0x55a0d0192135 <unknown>#7 0x55a0d019313e <unknown>#8 0x55a0d0434e4b <unknown>#9 0x55a0d0438dfa <unknown>#10 0x55a0d04216d5 <unknown>#11 0x55a0d0439a6f <unknown>#12 0x55a0d040569f <unknown>#13 0x55a0d045e098 <unknown>#14 0x55a0d045e262 <unknown>#15 0x55a0d046ff34 <unknown>#16 0x7f2b89c8f6ba <unknown>

OS: Ubuntu 23.04

Python version:

Python 3.11.4pip 23.3.2 from /home/nikolay/.local/lib/python3.11/site-packages/pip (python 3.11)

Just checked it with fresh venv, installed only selenium and cv2.Pip freeze:

attrs==23.2.0certifi==2023.11.17h11==0.14.0idna==3.6numpy==1.26.3opencv-python==4.9.0.80outcome==1.3.0.post0PySocks==1.7.1selenium==4.17.2sniffio==1.3.0sortedcontainers==2.4.0trio==0.24.0trio-websocket==0.11.1typing_extensions==4.9.0urllib3==2.2.0wsproto==1.2.0

Expected:Import cv2 and selenium and make them work together (as one would likely do).

Result:Selenium can no longer start chrome browser.

Code:

from selenium import webdriver# import cv2   # If I uncomment this, the problem happensbrowser = webdriver.Chrome()print('Loading page...')browser.get('https://snake.io')input()

IMPORTANT EDIT

Apparently the code works if I import cv2 AFTER chrome gets started.

from selenium import webdrivera = webdriver.Chrome()import cv2a.get('https://example.com')

Well that actually makes sense and doesn't help. BUT I also looked into cv2 init.py:

def bootstrap():    import sys    import copy    * code omitted *    for p in reversed(l_vars['PYTHON_EXTENSIONS_PATHS']):        sys.path.insert(1 if not applySysPathWorkaround else 0, p)

And actually there are several more lines where sys.path gets manipulated, so that is the only probable cause for the problem I got. Although I have no idea how am I the only one who has this bug (cause google search showed nothing). I'm trying to dig into it by myself but help would be appreciated (Although it seems this bug is kind of unique to my setup? And might be not reproducible).


Viewing all articles
Browse latest Browse all 98111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>