无法使用以下命令获取 google-chrome 的版本:powershell "$ErrorActionPreference='silentlycontinue' 使用 WebDriver 管理器和 Selenium

Could not get version for google-chrome with the command: powershell "$ErrorActionPreference='silentlycontinue' using WebDriver manager and Selenium

我正在尝试使用 python 创建一个脚本,将两种网站分开,一个包含 SPF,另一个包含 SPF,并使用 python 对它们进行分类,因此在一开始它工作得很好,但最近它给我一条消息错误,我找不到任何线索

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from concurrent.futures import ThreadPoolExecutor
import re
import requests
import json
from datetime import datetime
from colorama import Fore, Back, Style
import colorama
from webdriver_manager.chrome import ChromeDriverManager

colorama.init()
def checkCaptchaPresent(driver):
    captchaFound = True
    while captchaFound:
        try:
            driver.find_element_by_id("captcha-form")
            driver.set_window_position(0, 0)
        except:
            driver.set_window_position(20000, 0)
            captchaFound = False
            return 0

def requestSPF(url):
    response = requests.get("https://api.sparkpost.com/api/v1/messaging-tools/spf/query?domain={}".format(url)).json()

    for error in response['results']['errors']:
        if "does not have an SPF record" in error['message']:
            print(Fore.RED + "{} does not have an SPF record".format(url))
            return [url]
    
    print(Fore.GREEN + "{} have an SPF record".format(url))
    return []

chrome_options = Options()
PATH = "webdriver/chromedriver.exe"
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--window-size=1000,1000')
chrome_options.add_argument('log-level=3')
# chrome_options.add_argument("--user-data-dir=SFPInspector")

while True:
    driver = webdriver.Chrome(ChromeDriverManager().install(),options=chrome_options)
    driver.set_window_position(20000, 0)
    search_term = input("Enter search term: ")
    number_results = int(input("Max number of url to scrape: "))
    language_code = "en"

    driver.get('https://www.google.com/search?q={}&num={}&hl={}'.format(search_term, number_results+1, language_code))
    print('https://www.google.com/search?q={}&num={}&hl={}'.format(search_term, number_results+1, language_code))
    checkCaptchaPresent(driver)
    urls = driver.find_elements_by_xpath("//div[@id='search']/div/div/div[@class='g']//div[@class='yuRUbf']/a")
    websiteLink = []
    for url in urls:
        scrappedURL = url.get_attribute('href')
        print(scrappedURL)
        websiteLink.append(scrappedURL)

    filteredURL = []
    for i, url in enumerate(websiteLink):
        match = re.compile("^http.*com[/]")
        matchedURL = match.findall(url)
        
        filteredURL += matchedURL

    filteredURL = [url.replace('https:', '').replace('http:', '').replace('/', '') for url in filteredURL]

    noSPFURL = []
    with ThreadPoolExecutor(max_workers=int(10)) as pool:
        for res in pool.map(requestSPF, filteredURL):
            noSPFURL += res

    print(Style.RESET_ALL)
    driver.close()
    fileName = datetime.now().strftime("%d%m%Y-%H%M")
    print("Saving reports: report/{}_AllSite_{}.txt".format(''.join(e for e in search_term if e.isalnum()), fileName))
    with open('report/{}_AllSite_{}.txt'.format(''.join(e for e in search_term if e.isalnum()), fileName), 'w') as filehandle:
        for link in websiteLink:
            filehandle.write("{}\n".format(link))

    print("Saving reports: report/{}_NoSPF_{}.txt".format(''.join(e for e in search_term if e.isalnum()), fileName))
    with open('report/{}_NoSPF_{}.txt'.format(''.join(e for e in search_term if e.isalnum()), fileName), 'w') as filehandle:
        for link in noSPFURL:
            filehandle.write("{}\n".format(link))

输出信息如下:

====== WebDriver manager ======
Could not get version for google-chrome with the command:  powershell "$ErrorActionPreference='silentlycontinue' ; (Get-Item -Path "$env:PROGRAMFILES\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion ; if (-not $? -or $? -match $error) { (Get-Item -Path "$env:PROGRAMFILES(x86)\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion } if (-not $? -or $? -match $error) { (Get-Item -Path "$env:LOCALAPPDATA\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion } if (-not $? -or $? -match $error) { reg query "HKCU\SOFTWARE\Google\Chrome\BLBeacon" /v version } if (-not $? -or $? -match $error) { reg query "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome" /v version }"
Current google-chrome version is UNKNOWN
Get LATEST chromedriver version for UNKNOWN google-chrome
Trying to download new driver from https://chromedriver.storage.googleapis.com/98.0.4758.102/chromedriver_win32.zip
Driver has been saved in cache [C:\Users\dell\.wdm\drivers\chromedriver\win32.0.4758.102]
Enter search term: 

这个错误信息...

====== WebDriver manager ======
Could not get version for google-chrome with the command:  powershell "$ErrorActionPreference='silentlycontinue' ; (Get-Item -Path "$env:PROGRAMFILES\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion ; if (-not $? -or $? -match $error) { (Get-Item -Path "$env:PROGRAMFILES(x86)\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion } if (-not $? -or $? -match $error) { (Get-Item -Path "$env:LOCALAPPDATA\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion } if (-not $? -or $? -match $error) { reg query "HKCU\SOFTWARE\Google\Chrome\BLBeacon" /v version } if (-not $? -or $? -match $error) { reg query "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome" /v version }"
Current google-chrome version is UNKNOWN
Get LATEST chromedriver version for UNKNOWN google-chrome

...表示 was unable to retrieve the version of the installed browser within the system through any of the below commands and 查询:

  • Get-Item -Path "$env:PROGRAMFILES\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion
  • Get-Item -Path "$env:PROGRAMFILES(x86)\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion
  • Get-Item -Path "$env:LOCALAPPDATA\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion
  • reg query "HKCU\SOFTWARE\Google\Chrome\BLBeacon" /v version
  • reg query "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome" /v version

深入探讨

在以下讨论中详细讨论了此问题:

并最终通过 fix powershell determination and str concatenation

修复

解决方案

升级 to the latest version of webdriver-manager 3.5.4

如果您的 chrome 安装在 C:\Program Files (x86) 下,请阅读以下内容:

我的问题是 powershell 调用的命令:

(Get-Item -Path "$env:PROGRAMFILES(x86)\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion

尝试自己调用它,您会发现:

Get-Item : Cannot find path 'C:\Program Files(x86)\Google\Chrome\Application\chrome.exe' because it does not exist.
At line:1 char:2
+ (Get-Item -Path "$env:PROGRAMFILES(x86)\Google\Chrome\Application\chr ...
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Program File...tion\chrome.exe:String) [Get-Item], ItemNotFoundExcep
   tion
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand

问题出在Program Files(x86)之间省略了space 即它应该是

  • C:\Program Files (x86)

而不是

  • C:\Program Files(x86)windows下没有这样的文件夹OS

棘手的部分是在source code file中,space实际上存在于环境变量PROGRAMFILES和[=15=之间] :

        r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion',

我认为 raw-stringifying (r') 那个命令以某种方式擦掉了 space.


旁注:值得注意的是,一个可能的修复正在合并过程中,拉取请求者在评论 there:

As one can see, the reading the version from the registry is about 22.000 times faster that spawning powershell process to do the same thing. More importantly, the read chrome version will be correct (and not UNKNOWN)

一个简单的解决方案是卸载 google chrome 并在其他堆栈跟踪路径之一下重新安装它(例如 C:\Program Files