英文:
Accessing the `chrome://` pages inside Python without Selenium
问题
有`requests`和`urllib`页面可以在Python中访问`http(s)`协议页面,例如
```python
import requests
requests.get('stackoverflow.com')
但是对于chrome://
页面,例如chrome://settings/help
,url库无法工作,而这个:
import requests
requests.get('chrome://settings/help')
会引发错误:
InvalidSchema: No connection adapters were found for 'chrome://settings/help'
我猜测requests或urllib无法确定要使用哪个chrome以及浏览器的二进制可执行文件在哪里。因此,适配器无法轻松编码。
这里的目标是从机器上默认的chrome浏览器的chrome://settings/help
页面以pythonic的方式获取字符串Version 111.0.5563.146 (Official Build) (x86_64)
,例如:
从技术上讲,通过selenium
可以达到这个页面,例如
from selenium import webdriver
driver = webdriver.Chrome("./lib/chromedriver_111.0.5563.64")
driver.get('chrome://settings/help')
但是即使我们可以让selenium驱动程序访问chrome://settings/help
,.page_source
中缺少Version ...
的信息。
此外,除了chrome版本之外,访问chrome://
页面还将用于检索其他信息,例如这里。
虽然有一种方法调用Windows命令行函数来检索浏览器版本详细信息,例如这里,但该解决方案不能泛化并在Mac OS / Linux上工作。
<details>
<summary>英文:</summary>
There's the `requests` and `urllib` page that can access `http(s)` protocol pages in Python, e.g.
```python
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:
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. https://stackoverflow.com/questions/50880917/how-to-get-chrome-version-using-command-prompt-in-windows , the solution won't generalize and work on Mac OS / Linux.
答案1
得分: 1
如果你愿意尝试其他方式,
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())
或者根据你的Chrome路径可能是C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe
。
还有一个适用于linux/mac的版本,
$ /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version
以及一个linux版本
google-chrome --version
使用 subprocess.popen()
英文:
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()
答案2
得分: 1
你可以使用[Pyppeteer](https://pyppeteer.github.io/pyppeteer/)作为Selenium访问`chrome://`页面的替代方案
pip install pyppeteer asyncio
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) # 等待3秒
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())
输出将对应于你的Chrome应用程序:
Version 112.0.5615.87 (Official Build) (64-bit)
对于Linux/Mac,你可以根据Chrome应用程序的路径更改`executablePath`
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论