简体   繁体   中英

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

So I have made a program on one computer using selenium and that worked, Now using it in another computer I get this error:

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

Now this same issue was mention in:

Selenium python: Can not connect to the Service %s" % self.path

Selenium python: Can not connect to the Service %s" % self.path

Selenium and Python3 ChromeDriver raises Message: Can not connect to the Service chromedriver

however the solutions mentioned didnt work.

I am using chrome version 79 and have installed chromedriver 79, I tested writing chromedriver in command line and that works which means path is configured right, I have made sure 127.0.0.1 localhost is also in etc/hosts

Below is my code which works on my computer (so i doubt its an issue with the code):

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

In the last question I also tried this modification:

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

which would instead give me this almost identical error message:

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

I am unsure where the issue could lie.

I am using windows by the way.

EDIT: Things I tried and didn't work:

1- running everything as both admin and normal

2- re-installing chrome

3- using the beta-chroma 80 and webdriver 80

4- using normal chrome 79 and webdriver 79

5- Having both the script and the driver in the same directory (while using a correct path)

6- Having an external path and have it setup as needed

7- Using it in the PATH folder.

8- Adding "127.0.0.1 localhost" to etc/hosts

9- Running service test

I have ensured in every test that everything was in it's correct placement, I have ran a reboot before every new test, and they always give me the same error, which also happens to occur if I had an incorrect path as well, but once I ran the code for a service and it gave me a different error as I had my webdriver in C:/ which required admin privilages, however re-running the test again with the correct privilages gave back the same error

Update the issue isn't exclusive to the chrome driver. Even following setup instructions for either the Firefox or edge drivers end up on the same issues. It makes me suspect that the connection is facing some issue. I have tried running the test codes provided by Mozilla for the setup and it didn't work.

Unsure if that does help much or at all.

This error message...

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

...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context ie Chrome Browser session.


You need to take care of a couple of things:

  • Ensure that you have downloaded the exact format of the ChromeDriver binary from the download location pertaining to your underlying OS among:

    • chromedriver_linux64.zip : For Linux OS
    • chromedriver_mac64.zip : For Mac OSX
    • chromedriver_win32.zip : For Windows OS
  • Ensure that /etc/hosts file contains the following entry:

     127.0.0.1 localhost
  • Ensure that ChromeDriver binary have executable permission for the non-root user.

  • Ensure that you have passed the proper absolute path of ChromeDriver binary through the argument executable_path as follows:

     with webdriver.Chrome(executable_path=r'C:\\path\\to\\chromedriver.exe', chrome_options=chrome_options) as driver:
  • So your effective code block will be:

     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

Mandatory Considerations

Finally, to avoid incompatibility between the version of the binaries you are using ensure that:

  • Selenium is upgraded to current levels Version 3.141.59 .
  • ChromeDriver is updated to current ChromeDriver v79.0.3945.36 level.
  • Chrome is updated to current Chrome Version 79.0 level. (as per ChromeDriver v79.0 release notes )
  • Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
  • If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client .
  • Take a System Reboot .
  • Execute your @Test as non-root user.

References

You can find a couple of reference discussions in:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM