簡體   English   中英

selenium.common.exceptions.WebDriverException: 消息:無法通過 Selenium Python 使用 ChromeDriver Chrome 連接到服務錯誤

[英]selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service error using ChromeDriver Chrome through Selenium Python

所以我在一台計算機上使用 selenium 制作了一個程序並且工作正常,現在在另一台計算機上使用它我得到這個錯誤:

selenium.common.exceptions.WebDriverException:消息:無法連接到服務 chromedriver

現在在以下內容中提到了同樣的問題:

Selenium python:無法連接到服務 %s" % self.path

Selenium python:無法連接到服務 %s" % self.path

Selenium 和 Python3 ChromeDriver 引發消息:無法連接到服務 chromedriver

然而,提到的解決方案不起作用。

我正在使用 chrome 版本 79 並安裝了 chromedriver 79,我測試了在命令行中編寫 chromedriver 並且可以正常工作,這意味着路徑配置正確,我確保 127.0.0.1 localhost 也在 etc/hosts 中

下面是我在我的電腦上運行的代碼(所以我懷疑它的代碼有問題):

chrome_options = Options()   
chrome_options.add_argument("--headless")  
with webdriver.Chrome(chrome_options=chrome_options) as driver:
    driver.set_window_size(800, 460) # takes two arguments, width and height of the browser and it has to be called before using get()
    driver.execute_script("document.body.style.zoom='150%'")
    driver.get("file:\\"+url) # takes one argument, which is the url of the website you want to open
    driver.find_element_by_tag_name('body').screenshot(output)  # avoids scrollbar

在最后一個問題中,我也嘗試了這種修改:

chrome_options = Options()   
chrome_options.add_argument("--headless")  
with webdriver.Chrome("C:\\chromedriver.exe",chrome_options=chrome_options) as driver:
    driver.set_window_size(800, 460) # takes two arguments, width and height of the browser and it has to be called before using get()
    driver.execute_script("document.body.style.zoom='150%'")
    driver.get("file:\\"+url) # takes one argument, which is the url of the website you want to open
    driver.find_element_by_tag_name('body').screenshot(output)  # avoids scrollbar

這反而會給我這個幾乎相同的錯誤信息:

selenium.common.exceptions.WebDriverException:消息:無法連接到服務 C:\\chromedriver.exe

我不確定問題可能出在哪里。

順便說一下,我正在使用Windows。

編輯:我嘗試過但沒有奏效的事情:

1- 以管理員和普通身份運行一切

2-重新安裝鉻

3- 使用 beta-chroma 80 和 webdriver 80

4- 使用普通的 chrome 79 和 webdriver 79

5- 將腳本和驅動程序放在同一目錄中(同時使用正確的路徑)

6- 擁有外部路徑並根據需要進行設置

7- 在 PATH 文件夾中使用它。

8- 將“127.0.0.1 localhost”添加到 etc/hosts

9- 運行服務測試

我在每次測試中都確保一切都在正確的位置,我在每次新測試之前都進行了重新啟動,他們總是給我同樣的錯誤,如果我的路徑不正確也會發生這種情況,但是一旦我運行服務的代碼,它給了我一個不同的錯誤,因為我在 C:/ 中有我的網絡驅動程序,這需要管理員權限,但是使用正確的權限再次重新運行測試返回了相同的錯誤

更新問題並非 chrome 驅動程序獨有。 即使遵循 Firefox 或 Edge 驅動程序的設置說明,最終也會遇到相同的問題。 這讓我懷疑連接正面臨一些問題。 我試過運行 Mozilla 提供的測試代碼進行設置,但沒有奏效。

不確定這是否有很大幫助或根本沒有幫助。

這個錯誤信息...

selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver

...暗示ChromeDriver無法啟動/生成新的瀏覽上下文,Chrome 瀏覽器會話。


您需要注意以下幾點:

  • 確保您已從與您的底層操作系統相關的下載位置下載了准確格式的ChromeDriver二進制文件:

    • chromedriver_linux64.zip :適用於Linux 操作系統
    • chromedriver_mac64.zip :適用於Mac OSX
    • chromedriver_win32.zip :適用於Windows 操作系統
  • 確保/etc/hosts文件包含以下條目:

     127.0.0.1 localhost
  • 確保ChromeDriver二進制文件對非 root用戶具有可執行權限。

  • 確保您已通過參數executable_path傳遞ChromeDriver二進制executable_path的正確絕對路徑,如下所示:

     with webdriver.Chrome(executable_path=r'C:\\path\\to\\chromedriver.exe', chrome_options=chrome_options) as driver:
  • 所以你的有效代碼塊將是:

     options = Options() options.add_argument("--headless") options.add_argument('--no-sandbox') # Bypass OS security model options.add_argument('--disable-gpu') # applicable to windows os only options.add_argument("--disable-dev-shm-usage") # overcome limited resource problems options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) with webdriver.Chrome(executable_path=r'C:\\path\\to\\chromedriver.exe', options=options) as driver: driver.set_window_size(800, 460) # takes two arguments, width and height of the browser and it has to be called before driver.execute_script("document.body.style.zoom='150%'") driver.get("file:\\\\"+url) # takes one argument, which is the url of the website you want to open driver.find_element_by_tag_name('body').screenshot(output) # avoids scrollbar

強制性注意事項

最后,為避免您使用的二進制文件版本之間的不兼容,請確保:

  • Selenium升級到當前級別Version 3.141.59
  • ChromeDriver更新到當前ChromeDriver v79.0.3945.36級別。
  • Chrome已更新到當前的Chrome 版本 79.0級別。 (根據ChromeDriver v79.0 發行說明
  • 通過IDE清理項目工作區並僅使用所需的依賴項重建項目。
  • 如果您的基本Web 客戶端版本太舊,請卸載它並安裝最新的 GA 和發布版本的Web 客戶端
  • 進行系統重啟
  • 非 root用戶身份執行@Test

參考

您可以在以下位置找到一些參考討論:

暫無
暫無

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

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