访问`chrome://`页面而无需使用Selenium的Python方法

huangapple go评论154阅读模式
英文:

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),例如:

访问`chrome://`页面而无需使用Selenium的Python方法


从技术上讲,通过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&#39;s the `requests` and `urllib` page that can access `http(s)` protocol pages in Python, e.g. 


```python
import requests
requests.get(&#39;stackoverflow.com&#39;)

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

import requests

requests.get(&#39;chrome://settings/help&#39;)

throws the error:

InvalidSchema: No connection adapters were found for &#39;chrome://settings/help&#39;

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:

访问`chrome://`页面而无需使用Selenium的Python方法


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

from selenium import webdriver

driver = webdriver.Chrome(&quot;./lib/chromedriver_111.0.5563.64&quot;)
driver.get(&#39;chrome://settings/help&#39;)

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&#39;wmic datafile where name=&quot;C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe&quot; get Version /value&#39;,
    shell=True
)
print(output.decode(&#39;utf-8&#39;).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({
        &quot;headless&quot;: False,
        &quot;executablePath&quot;: &quot;C:/Program Files/Google/Chrome/Application/chrome.exe&quot;
    })
    page = await browser.newPage()
    await page.goto(&#39;chrome://settings/help&#39;)
    time.sleep(3) # wait 3s
    chromeVersion = await page.evaluate(&#39;&#39;&#39;
      document.querySelector(&#39;settings-ui&#39;)
      .shadowRoot.querySelector(&#39;settings-main&#39;)
      .shadowRoot.querySelector(&#39;settings-about-page&#39;)
      .shadowRoot.querySelector(&#39;div.secondary&#39;).innerText
    &#39;&#39;&#39;)
    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.

huangapple
  • 本文由 发表于 2023年4月4日 12:28:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定