3

There's the requests and urllib page that can access http(s) protocol pages in Python, e.g.

import requests
requests.get('stackoverflow.com')

but when it comes to chrome:// pages, e.g. chrome://settings/help, the url libraries won't work and this:

import requests

requests.get('chrome://settings/help')

throws the error:

InvalidSchema: No connection adapters were found for 'chrome://settings/help'

I guess there's no way for requests or urllib to determine which chrome to use and where's the binary executable file for the browser. So the adapter can't be easily coded.

The goal here is to pythonically obtain the string Version 111.0.5563.146 (Official Build) (x86_64) from the chrome://settings/help page of the default chrome browser on the machine, e.g. it looks like this:

enter image description here


Technically, it is possible to get to the page through selenium e.g.

from selenium import webdriver

driver = webdriver.Chrome("./lib/chromedriver_111.0.5563.64")
driver.get('chrome://settings/help')

But even if we can get the selenium driver to get to the chrome://settings/help, the .page_source is information for Version ... is missing.

Also, other than the chrome version, the access to chrome:// pages would be used to retrieve other information, e.g. https://www.ghacks.net/2012/09/04/list-of-chrome-urls-and-their-purpose/


While's there's a way to call a Windows command line function to retrieve the browser version details, e.g. How to get chrome version using command prompt in windows , the solution won't generalize and work on Mac OS / Linux.

alvas
  • 115,346
  • 109
  • 446
  • 738

2 Answers2

1

If you are open to something other than requests,

import subprocess
output = subprocess.check_output(
    r'wmic datafile where name="C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" get Version /value',
    shell=True
)
print(output.decode('utf-8').strip())

or depending on the path to Chrome on your box, C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe.

There's also a linux/mac version of the above,

$ /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version

and a linux version

google-chrome --version

using subprocess.popen()

JeffC
  • 22,180
  • 5
  • 32
  • 55
1

You can use Pyppeteer as Selenium's alternative for accessing the chrome:// pages

pip install pyppeteer asyncio

Example for Windows OS:

from pyppeteer import launch
import time
import asyncio
import nest_asyncio

nest_asyncio.apply()

async def main():
    browser = await launch({
        "headless": False,
        "executablePath": "C:/Program Files/Google/Chrome/Application/chrome.exe"
    })
    page = await browser.newPage()
    await page.goto('chrome://settings/help')
    time.sleep(3) # wait 3s
    chromeVersion = await page.evaluate('''
      document.querySelector('settings-ui')
      .shadowRoot.querySelector('settings-main')
      .shadowRoot.querySelector('settings-about-page')
      .shadowRoot.querySelector('div.secondary').innerText
    ''')
    await browser.close()
    print(chromeVersion)

asyncio.get_event_loop().run_until_complete(main())

The output will correspond to your Chrome Application:

Version 112.0.5615.87 (Official Build) (64-bit)

For Linux/Mac, you can change executablePath based on the Chrome application's path.

Jordy
  • 1,802
  • 2
  • 6
  • 25