Home > Software engineering >  Selenium starts the browser but shows an error as Message: Can not connect to the Service
Selenium starts the browser but shows an error as Message: Can not connect to the Service

Time:12-26

The code starts the browser, stops at this step (line 5), and after a while throws an error:

selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service C:\Program Files\Mozilla Firefox\firefox.exe

from selenium import webdriver
from selenium.webdriver.firefox.service import Service

s = Service(r'C:\Program Files\Mozilla Firefox\firefox.exe')
driver = webdriver.Firefox(service=s)
driver.get('http://www.google.com')
myPageTitle = driver.title
print(myPageTitle)
driver.quit()

Firefox - 95.0.2 Selenium - 4.1.0

I tried with chrome, same problem

Does anyone know what the problem is and how to solve it?

CodePudding user response:

As an argument to Service() instead of the executable, you need to pass the absolute location of the GeckoDriver executable which can be downloaded from mozilla/geckodriver page.

So your effective code block will be:

from selenium import webdriver
from selenium.webdriver.firefox.service import Service

s = Service(r'C:\path\to\geckodriver.exe')
driver = webdriver.Firefox(service=s)
driver.get('http://www.google.com')
myPageTitle = driver.title
print(myPageTitle)
driver.quit()
  • Related