Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: Could not get version for Chrome with this command #205

Closed
shubhank-saxena opened this issue Jun 17, 2021 · 10 comments · Fixed by #300
Closed

ValueError: Could not get version for Chrome with this command #205

shubhank-saxena opened this issue Jun 17, 2021 · 10 comments · Fixed by #300

Comments

@shubhank-saxena
Copy link

shubhank-saxena commented Jun 17, 2021

I am trying to write a basic sanity check for a django application using selenium.
This is the code

import pytest

from django.test import LiveServerTestCase
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager


class TestBrowser1(LiveServerTestCase):
    def test_example(self):
        driver = webdriver.Chrome(ChromeDriverManager().install())
        driver.get(("%s%s" % (self.live_server_url, "/admin/")))
        assert "Log in | Django site admin" in driver.title

I am getting the following error after running pytest

 ValueError: Could not get version for Chrome with this command: google-chrome --version || google-chrome-stable --version

Error replicated in Mac M1 as well as Ubuntu 18.04

@liquidgenius
Copy link

liquidgenius commented Jun 23, 2021

Confirming the same issue in Google Colab environment.

!pip install selenium webdriver_manager

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager


def test():
    driver = webdriver.Chrome(ChromeDriverManager().install())

test()

Output:

====== WebDriver manager ======
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-fbd55f77ab7c> in <module>()
----> 1 test()

3 frames
/usr/local/lib/python3.7/dist-packages/webdriver_manager/utils.py in chrome_version(browser_type)
    153 
    154     if not version:
--> 155         raise ValueError(f'Could not get version for Chrome with this command: {cmd}')
    156     current_version = version.group(0)
    157     return current_version

ValueError: Could not get version for Chrome with this command: google-chrome --version || google-chrome-stable --version

@liquidgenius
Copy link

liquidgenius commented Jun 23, 2021

In the case of Google Colab, running on Linux, these are the functions associated with the error in webdriver_manager/utils.py The associated functions are:

def os_name():
    pl = sys.platform
    if pl == "linux" or pl == "linux2":
        return OSType.LINUX
    elif pl == "darwin":
        return OSType.MAC
    elif pl == "win32":
        return OSType.WIN

def linux_browser_apps_to_cmd(*apps: str) -> str:
    """Create chrome version command from browser app names.

    Result command example:
        chromium --version || chromium-browser --version
    """
    ignore_errors_cmd_part = ' 2>/dev/null' if os.getenv('WDM_LOG_LEVEL') == '0' else ''
    return ' || '.join(list(map(lambda i: f'{i} --version{ignore_errors_cmd_part}', apps)))


def chrome_version(browser_type=ChromeType.GOOGLE):
    pattern = r'\d+\.\d+\.\d+'

    cmd_mapping = {
        ChromeType.GOOGLE: {
            OSType.LINUX: linux_browser_apps_to_cmd('google-chrome', 'google-chrome-stable'),
            OSType.MAC: r'/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version',
            OSType.WIN: r'reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version'
        },
        ChromeType.CHROMIUM: {
            OSType.LINUX: linux_browser_apps_to_cmd('chromium', 'chromium-browser'),
            OSType.MAC: r'/Applications/Chromium.app/Contents/MacOS/Chromium --version',
            OSType.WIN: r'reg query "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome" /v version'
        },
        ChromeType.MSEDGE: {
            OSType.MAC: r'/Applications/Microsoft\ Edge.app/Contents/MacOS/Microsoft\ Edge --version',
            OSType.WIN: r'reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Edge\BLBeacon" /v version',
        }
    }

    cmd = cmd_mapping[browser_type][os_name()]
    version = None
    with os.popen(cmd) as stream:
        stdout = stream.read()
        version = re.search(pattern, stdout)

    if not version:
        raise ValueError(f'Could not get version for Chrome with this command: {cmd}')
    current_version = version.group(0)
    return current_version

It looks like Colab is defaulting to cmd_mapping[ChromeType.CHROMIUM][OSType.LINUX]
Manual execution of the command in Colab's underlying OS yields:

!chromium --version || chromium-browser --version

/bin/bash: chomium: command not found
/bin/bash: chromium-browser: command not found

This leads me to believe that there may have been a silent failure to install chromium.

@HassanAlRabea
Copy link

Hi! I'm also running into the same issue using windows + jupyter notebook:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
browser = webdriver.Chrome(ChromeDriverManager().install())

Output:

ValueError: Could not get version for Chrome with this command: reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version

@ghost
Copy link

ghost commented Jun 28, 2021

Hello! Is there a solution to this problem.

import pytest
from selenium import webdriver

from webdriver_manager.chrome import ChromeDriverManager
@pytest.fixture(scope="session")
def browser():
    """Init driver from test Chrome """
    driver = webdriver.Chrome(ChromeDriverManager().install())
    yield driver
    driver.quit()

output:
ValueError: Could not get version for Chrome with this command: reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version

@memorydoor
Copy link

Getting the same error, for some reason stream.read() is returning blank
with os.popen(cmd) as stream:
stdout = stream.read()

@hansingjunitra
Copy link

Hello! I encountered the same problem in MacOS. Current chrome_driver run is 92.0.45, and I have make sure that I installed the chrome driver using
brew install --cask chromedriver
Script :

def scrape(self):
        # INSTANTIATION #
        options = Options()
        options.headless = True
        browser = webdriver.Chrome(ChromeDriverManager().install(), options=options)

Error:

  File "/Users/user/Library/Python/3.8/lib/python/site-packages/webdriver_manager/utils.py", line 155, in chrome_version
    raise ValueError(f'Could not get version for Chrome with this command: {cmd}')
ValueError: Could not get version for Chrome with this command: /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version

@beneko
Copy link

beneko commented Sep 24, 2021

The solution to this error is to add the path C:\Windows\System32\ to the Environment Variables:

Right click the My Computer icon then choose properties.
Select the Advanced tab and then click Environment Variables.
At the system variables panel, choose Path then click the Edit button.
Add C:\Windows\System32\, the paths are separated by a semicolon.

@Mlad-en
Copy link

Mlad-en commented Sep 28, 2021

Hi,

I'm replicating the same error on Garuda Linux. On Windows, @beneko 's solution works for me.

I'm also replicating that the following code seems to be the reason:
version = None with os.popen(cmd) as stream: stdout = stream.read() version = re.search(pattern, stdout)
The error that is raised by the command attempted for me is: /bin/sh: line 1: google-chrome-stable: command not found

Running the command in bash yields in the same error, but running it with fish, I get the following output:
chaotic-aur/google-chrome 94.0.4606.61-1 /usr/bin/google-chrome-stable

This output matches the regex pattern used and will return match='94.0.4606'

@paulschmeida
Copy link

Subprocess might not properly redirect output stream when executing cli commands to get browser version. This is what's happening to me when my scripts are packaged with pyinstaller and I believe it's the same issue with Jupyter.
A solution on Windows is would be to avoid using subprocess and use built-in module winreg to check registry key.
For now I just have this as a separate function:

import winreg


def get_chrome_version():
    """Reads current Chrome version from registry."""
    reg_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Google\Chrome\BLBeacon')
    version, _ = winreg.QueryValueEx(reg_key, 'version')
    winreg.CloseKey(reg_key)
    return version

driver = webdriver.Chrome(ChromeDriverManager(version=get_chrome_version()).install())

@SergeyPirogov If you're interested I can work that into webdriver-manager and create a pull request. I know it's only for Windows, but at least it's more Pythonic that using subprocess.

@aleksandr-kotlyar
Copy link
Collaborator

aleksandr-kotlyar commented Feb 10, 2022

Too many different problems in one issue.
I will try to answer all of them from first to last.
Let's get started.

1 Question

I am trying to write a basic sanity check for a django application using selenium. This is the code

import pytest

from django.test import LiveServerTestCase
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager


class TestBrowser1(LiveServerTestCase):
    def test_example(self):
        driver = webdriver.Chrome(ChromeDriverManager().install())
        driver.get(("%s%s" % (self.live_server_url, "/admin/")))
        assert "Log in | Django site admin" in driver.title

I am getting the following error after running pytest

 ValueError: Could not get version for Chrome with this command: google-chrome --version || google-chrome-stable --version

Error replicated in Mac M1 as well as Ubuntu 18.04

1 Answer

@shubhank-saxena may be you use 'chromium' browser? If so - try this example for chromium from Readme.md

#### Use with Chromium
```python
# selenium 3
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.utils import ChromeType
driver = webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install())
```
```python
# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.utils import ChromeType
driver = webdriver.Chrome(service=Service(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()))
```
Or just set google-chrome location into the PATH and then 'google-chrome --version' will start to work.

2 Question

Confirming the same issue in Google Colab environment.

!pip install selenium webdriver_manager

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager


def test():
    driver = webdriver.Chrome(ChromeDriverManager().install())

test()

Output:

====== WebDriver manager ======
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-fbd55f77ab7c> in <module>()
----> 1 test()

3 frames
/usr/local/lib/python3.7/dist-packages/webdriver_manager/utils.py in chrome_version(browser_type)
    153 
    154     if not version:
--> 155         raise ValueError(f'Could not get version for Chrome with this command: {cmd}')
    156     current_version = version.group(0)
    157     return current_version

ValueError: Could not get version for Chrome with this command: google-chrome --version || google-chrome-stable --version

2 Answer

@liquidgenius Google Colab doesn't have google-chrome - so answer is the same as previous - use webdriver-manager with CHROMIUM config:

#### Use with Chromium
```python
# selenium 3
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.utils import ChromeType
driver = webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install())
```
```python
# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.utils import ChromeType
driver = webdriver.Chrome(service=Service(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()))
```

Also - may be chromium is not installed properly in Google Colab, so try to install it:

!apt-get update
!apt-get install chromium-browser

And then execute the example webdriver-manager for Chromium above.

Full code to prove that webdriver-manager can detect Chromium-browser in Google Colab:

Google Colab
!chrome --version
!chromium --version
!chrome-browser --version
!chromium-browser --version

/bin/bash: chrome: command not found
/bin/bash: chromium: command not found
/bin/bash: chrome-browser: command not found
/bin/bash: chromium-browser: command not found

!apt-get update
!apt-get install chromium-browser
!chromium-browser --version

Chromium 97.0.4692.71 Built on Ubuntu , running on Ubuntu 18.04

!pip install webdriver-manager
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.utils import ChromeType
driver_path = ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()
print(driver_path)

====== WebDriver manager ======
Current chromium version is 97.0.4692
Get LATEST chromedriver version for 97.0.4692 chromium
There is no [linux64] chromedriver for browser in cache
Trying to download new driver from https://chromedriver.storage.googleapis.com/97.0.4692.71/chromedriver_linux64.zip
Driver has been saved in cache [/root/.wdm/drivers/chromedriver/linux64/97.0.4692.71]
/root/.wdm/drivers/chromedriver/linux64/97.0.4692.71/chromedriver

Question 3

Hi! I'm also running into the same issue using windows + jupyter notebook:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
browser = webdriver.Chrome(ChromeDriverManager().install())

Output:

ValueError: Could not get version for Chrome with this command: reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version

Answer 3

@HassanAlRabea this issue for windows is fixed now at master branch. Wait for 3.5.3 release and update for it. Please make separate issue if it will not work - that's topic about another problem.

Question 4

Hello! Is there a solution to this problem.

import pytest
from selenium import webdriver

from webdriver_manager.chrome import ChromeDriverManager
@pytest.fixture(scope="session")
def browser():
    """Init driver from test Chrome """
    driver = webdriver.Chrome(ChromeDriverManager().install())
    yield driver
    driver.quit()

output: ValueError: Could not get version for Chrome with this command: reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version

Answer 4

@ghost Update to 3.5.3 when it will be released. Fix is already at master branch.

Question 5

Hello! I encountered the same problem in MacOS. Current chrome_driver run is 92.0.45, and I have make sure that I installed the chrome driver using brew install --cask chromedriver Script :

def scrape(self):
        # INSTANTIATION #
        options = Options()
        options.headless = True
        browser = webdriver.Chrome(ChromeDriverManager().install(), options=options)

Error:

  File "/Users/user/Library/Python/3.8/lib/python/site-packages/webdriver_manager/utils.py", line 155, in chrome_version
    raise ValueError(f'Could not get version for Chrome with this command: {cmd}')
ValueError: Could not get version for Chrome with this command: /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version

Answer 5

@hansingjunitra May be you use Chromium browser? Same answer - try Chromium config then

#### Use with Chromium
```python
# selenium 3
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.utils import ChromeType
driver = webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install())
```
```python
# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.utils import ChromeType
driver = webdriver.Chrome(service=Service(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()))
```

Or make a separate issue.

Question 6

Subprocess might not properly redirect output stream when executing cli commands to get browser version. This is what's happening to me when my scripts are packaged with pyinstaller and I believe it's the same issue with Jupyter. A solution on Windows is would be to avoid using subprocess and use built-in module winreg to check registry key. For now I just have this as a separate function:

import winreg


def get_chrome_version():
    """Reads current Chrome version from registry."""
    reg_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Google\Chrome\BLBeacon')
    version, _ = winreg.QueryValueEx(reg_key, 'version')
    winreg.CloseKey(reg_key)
    return version

driver = webdriver.Chrome(ChromeDriverManager(version=get_chrome_version()).install())

@SergeyPirogov If you're interested I can work that into webdriver-manager and create a pull request. I know it's only for Windows, but at least it's more Pythonic that using subprocess.

Answer 6

@paulschmeida determining versions on windows is already at master - update to 3.5.3 when it comes to pypi. Or you can test current master on your machine. Please create a separate issue if you will have a questions - because the topic is about another problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants