@gdap (gdap)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

ChromeDriverを起動できない

解決したいこと

SeleniumでGoogle検索を行うPythonのコードをスクリプトファイルで保存し、wsl2で実行しようと思ったのですが以下のエラーが発生しました。

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

WSL2からWindows10のChromeを操作したいと思い、chromedriver.exeをダウンロードしました。パスはコード内に指定しました。

該当するソースコード

from selenium.webdriver import Chrome, ChromeOptions, Remote
from selenium.webdriver.common.keys import Keys

chrome_path = '/mnt/c/Users/ユーザー名/OneDrive/デスクトップ/crawler/chromedriver.exe'
options = ChromeOptions()
driver = Chrome(options=options, executable_path=chrome_path)

driver.get('https://www.google.co.jp/')

assert 'Google' in driver.title

input_element = driver.find_element_by_name('q')
input_element.send_keys('Python')
input_element.send_keys(keys.RETURN)

assert 'Python' in driver.title

for h3 in driver.find_element_by_css_selector('a > h3'):
    a = h3.find_element_by_xpath('..')
    print(h3.text)
    print(a.get_attribute('href'))

driver.quit()

自分で試したこと

・GoogleChromeはバージョン89.0.4389.82なので古いバージョンのChromeDriverを削除し、バージョン 89.0.4389.23をダウンロード、インストールしました。
・他にもググったら、hostsファイルを修正すればいいとかいてあったので、hostsファイルを見ると

127.0.0.1 localhost

としっかり書いてありました。

これといった原因がわかりません。解決方法を教えてください。

0 likes

No Answers yet.

Your answer might help someone💌