簡體   English   中英

"<i>Selenium Python code that used to work now opens browser and then gets stuck in &quot;data:,&quot;.<\/i>以前可以工作的 Selenium Python 代碼現在打開瀏覽器,然后卡在“data:”中。<\/b> <i>Says &quot;chrome not reachable&quot;<\/i>說“鉻無法訪問”<\/b>"

[英]Selenium Python code that used to work now opens browser and then gets stuck in "data:,". Says "chrome not reachable"

編輯:我剛剛意識到我最近重新安裝了 Chrome,所以這可能是問題所在。

我認為 Selenium 期望在 Appdata 中找到 Chrome,但它不存在。 我嘗試將 C:\\Program Files\\Google\\Chrome\\ 中的 Application 文件夾復制到 C:\\Users\\hougy\\AppData\\Local\\Google\\Chrome 但這並不能解決問題。

我還嘗試向它展示 Chrome 的安裝位置,但它也不起作用:

options = webdriver.ChromeOptions()
options.binary_location = 'C:\Program Files\Google\Chrome\Application\chrome.exe'
browser = webdriver.Chrome(options=options)

我嘗試了上面代碼的變體,但它們都不起作用。

原始問題:

在過去的幾個月里,我的代碼運行良好。 然后我昨天運行它時,下載新的chromedriver后,瀏覽器打開但網頁沒有加載,它卡在地址“data:”中。

這是一個具有相同問題的簡化版本:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from lxml import etree
import csv
import time
from datetime import datetime
import requests

#options = webdriver.ChromeOptions()
#options.add_experimental_option('excludeSwitches', ['enable-logging'])
#options.add_argument('--no-sandbox')
#options.add_argument("start-maximized")
#browser = webdriver.Chrome(options=options)
browser = webdriver.Chrome()

quote = 'https://www.fundamentus.com.br/detalhes.php?papel=BBAS3'
browser.get(quote)

我嘗試將 browser = webdriver.Chrome() 替換為上面注釋的代碼,因為這里有人建議它,但結果是一樣的。 唯一的區別是它似乎忽略了一些與藍牙相關的錯誤消息,這些錯誤消息始終存在並且不是問題的原因。

我嘗試遷移到最新版本的 Python,然后更新 pip 並重新下載所有庫。 我還更新了 Chrome 並使用了最新的穩定版 chromedriver。

我嘗試在我的第二台 PC 上運行代碼,它可以在相同的 Python、Chrome、chromedriver 和庫版本上運行。

這是我運行它時得到的結果:

python .\seleniumTest.py

DevTools listening on ws://192.168.0.104:63785/devtools/browser/5d07327b-ae87-49ee-be00-b9cb0f055b74
[5156:5772:1101/160103.660:ERROR:chrome_browser_main_extra_parts_metrics.cc(230)] crbug.com/1216328: Checking Bluetooth availability started. Please report if there is no 
report that this ends.
[5156:5772:1101/160103.664:ERROR:chrome_browser_main_extra_parts_metrics.cc(233)] crbug.com/1216328: Checking Bluetooth availability ended.
[5156:13960:1101/160103.681:ERROR:device_event_log_impl.cc(214)] [16:01:03.680] Bluetooth: bluetooth_adapter_winrt.cc:1073 Getting Default Adapter failed.
[5156:5772:1101/160103.690:ERROR:chrome_browser_main_extra_parts_metrics.cc(236)] crbug.com/1216328: Checking default browser status started. Please report if there is no 
report that this ends.
[5156:5772:1101/160103.714:ERROR:chrome_browser_main_extra_parts_metrics.cc(240)] crbug.com/1216328: Checking default browser status ended.
[5156:13960:1101/160126.589:ERROR:device_event_log_impl.cc(214)] [16:01:26.588] USB: usb_service_win.cc:258 Failed to get device driver name: Element not found. (0x490)
Traceback (most recent call last):
  File "C:\Users\hougy\OneDrive\Documentos\plij\seleniumTest.py", line 14, in <module>
    browser = webdriver.Chrome()
  File "C:\Users\hougy\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__
    super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
  File "C:\Users\hougy\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 93, in __init__
    RemoteWebDriver.__init__(
  File "C:\Users\hougy\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 266, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\hougy\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 357, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\hougy\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 418, in execute
    self.error_handler.check_response(response)
  File "C:\Users\hougy\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: chrome not reachable
Stacktrace:
Backtrace:
        Ordinal0 [0x006006F3+2492147]
        Ordinal0 [0x00599BD1+2071505]
        Ordinal0 [0x004A2340+1057600]
        Ordinal0 [0x0049739D+1012637]
        Ordinal0 [0x004BFF64+1179492]
        Ordinal0 [0x004BC43B+1164347]
        Ordinal0 [0x004B9C9F+1154207]
        Ordinal0 [0x004E9DFF+1351167]
        Ordinal0 [0x004E9A6A+1350250]
        Ordinal0 [0x004E568B+1332875]
        Ordinal0 [0x004C21D4+1188308]
        Ordinal0 [0x004C302F+1191983]
        GetHandleVerifier [0x007867A6+1545030]
        GetHandleVerifier [0x0083105C+2243580]
        GetHandleVerifier [0x0068BC97+518199]
        GetHandleVerifier [0x0068AD80+514336]
        Ordinal0 [0x0059ED2D+2092333]
        Ordinal0 [0x005A2EE8+2109160]
        Ordinal0 [0x005A3022+2109474]
        Ordinal0 [0x005ACB71+2149233]
        BaseThreadInitThunk [0x76736359+25]
        RtlGetAppContainerNamedObjectPath [0x773187A4+228]
        RtlGetAppContainerNamedObjectPath [0x77318774+180]

當您之前執行您的程序時, 瀏覽器和ChromeDriver是完全同步的。 因此它執行得很好。

但是目前 Google Chrome 瀏覽器正在更新到版本 95.0.4638.69您的程序沒有找到匹配的ChromeDriver來驅動 Chrome 瀏覽器。

因此,您會在地址欄中看到data:,


解決方案

使用關鍵的executable_path指向下載的ChromeDriver匹配版本,如下所示:

browser = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')

我無法解決它,但發現了一個解決方法:使用 Firefox 和 geckodriver。 之前每當 Chrome 更新時,我都會遇到很多問題,所以我希望這次更改會有所改善,因為我只使用 Firefox。

3個月后我終於解決了這個問題。 我認為大多數收到相同錯誤消息的人都不會發生這種情況,但這是發生在我身上的事情:

Surfshark VPN 更新了他們的應用程序,最新版本使我的 Selenium 腳本無法找到 geckodriver 和 chromedriver。 我一卸載 Surfshark 應用程序,它就立即重新開始工作。 僅僅殺死進程是不夠的。

我現在通過 OpenVPN 應用程序使用 Surfshark,這個應用程序不會阻止我的腳本工作。 Surfshark 應用程序具有更多功能,但到目前為止我無法同時使用這兩種功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM