0

I'm currently trying to run a Chrome WebDriver with Selenium in a python script but I'm currently generating the error:

selenium.common.exceptions.WebDriverException: Message: chrome not reachable

The way I've set up the script is a bit weird. The server I want to run it on is using Windows, but it doesn'e have any python versions downloaded, so what I'm doing is editing and debugging the script on my personal computer, which also uses Windows, and then turning the script into a windows executable using pyinstaller. The executable is then put on the remote server and I run it there.

The portion of code for generating the ChromeDriver that I'm using is as follows:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("download.default_directory={}".format(os.getcwd()))
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument("--disable-setuid-sandbox")
options.add_argument("--remote-debugging-port=9222")
options.binary_location = ".\\chrome-win32\\chrome.exe"
service = Service(executable_path=".\\chrome-win32\\driver\\chromedriver.exe")
driver = webdriver.Chrome(service=service, options=options)

I have a folder named "chrome-win32" in the directory where the executable is stored that holds the chrom executable and I also have a folder named "driver" in there that holds the chromedriver executable. All this is to say that I don't think that there's any problems with my file structure.

I have double and triple checked my chrome and chrome driver versions. Both are the most recent downloads of the stable versions released on Chrome for Testing and both are the windows 32 bit releases.

All three of the mentioned executables have permission to execute.

I have tried looking at similar questions here on stack overflow, but most of the answers I've seen boil down to not having compabtible version of chrome and chromedriver, which I know is not the case here.

I've tried adding some arguments to Options however none of them have really helped, but I kept the options that I've tried in the code in case I'm doing something wrong or unnecessary.

  • There's some options there you shouldn't set... "--no-sandbox" (very dangerous... only set if needed, which you shouldn't in Windows) "--remote-debugging-port=9222" this is for attaching to a debug session of the Chrome browser... don't set that unless you are running the site in an IDE. Removing that option will let chromedriver/chrome set the port and session ID used. (and not familiar with "--disable-setuid-sandbox", but that shouldn't be needed either) If you still have issues, check to be sure Chrome has been launched manually at least once. – pcalkins Jul 24 '23 at 19:30
  • @pcalkins I just tried opening Chrome on the server and it kept crashing for some reason, so thank you for advising me to try that! I switched to Edge as I saw a few Edge processes already running on the server and it is working as intended now. I also pared down my arguments to just "--headless=new" and the default downloads one. – Nicholas Curtis Jul 24 '23 at 23:40
  • `--headless=new` isn't in @pcalkins comment :) but suggested in my [answer](https://stackoverflow.com/a/76757580/7429447) – undetected Selenium Jul 25 '23 at 07:25

1 Answers1

1

Remove the additional arguments added through the instance of Options() unless mandatory:

  • --disable-gpu
  • --no-sandbox
  • --disable-setuid-sandbox
  • --disable-software-rasterizer
  • --remote-debugging-port=9222

Further, replace --headless with --headless=new as the new Headless way


After all, if you are using Selenium v4.6 or above you don't have to explicitly use service keyword anymore as integrated Selenium Manager can silently download the matching ChromeDriver

Your minimal code block can be:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("download.default_directory={}".format(os.getcwd()))
options.add_argument("--headless=new")
options.binary_location = ".\\chrome-win32\\chrome.exe"
driver = webdriver.Chrome(options=options)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352