Home > database >  Selenium Python scripts: Chrome not reachable
Selenium Python scripts: Chrome not reachable

Time:02-04

I'm currently running a selenium python script on my EC2 machine. Google-chrome version (100.0.4896.60) Chrome driver (100.0.4896.60) Red hat (8.7) Python (3.6.8) Selenium (3.141.0) Pytest (7.0.1)

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

#argument to switch off suid sandBox and no sandBox in Chrome
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument("--remote-debugging-port=9222")
# chrome_options.add_argument('--start-maximized')
chrome_options.add_argument('--headless')
#chrome_options.add_argument("--window-size=1920,1080")
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-popup-blocking')
chrome_options.add_argument("--incognito")
userAgent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36"
chrome_options.add_argument(f'user-agent={userAgent}')
chrome_options.add_argument('ignore-certificate-errors')


driver = webdriver.Chrome("/usr/local/share/chromedriver", options=chrome_options)
driver.get('http://www.google.com')
print(driver.title)
driver.quit()

When I run the script, I get the following error.

================================================================================================================== ERRORS ===================================================================================================================
_________________________________________________________________________________________________________ ERROR collecting main.py __________________________________________________________________________________________________________
main.py:21: in <module>
    driver = webdriver.Chrome("/usr/local/share/chromedriver", options=chrome_options)
/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py:81: in __init__
    desired_capabilities=desired_capabilities)
/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py:157: in __init__
    self.start_session(capabilities, browser_profile)
/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py:252: in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py:321: in execute
    self.error_handler.check_response(response)
/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py:242: in check_response
    raise exception_class(message, screen, stacktrace)
E   selenium.common.exceptions.WebDriverException: Message: chrome not reachable
========================================================================================================== short test summary info ==========================================================================================================
ERROR main.py - selenium.common.exceptions.WebDriverException: Message: chrome not reachable
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

There was an update to google-chrome on my red hat server that caused it to upgrade to version 109, I was told to downgrade version back to 100, so avoid any breaks to our scripts in the short term. Now the versions of google-chrome and chromedriver are matching but I'm still seeing error that chrome cannot be reached. I already ensured that chrome driver is at correct path as well as correct permissions. I set permissions on google chrome using "chmod x chromedriver". Not sure what else to do.

The expected outcome of this script is very simple, as this is not my main code but an example. When running any script on this machine, I get the same error.

CodePudding user response:

You don't need all those arguments. To enable headless mode barely you need the following arguments:

  • chrome_options.add_argument('--headless')
  • chrome_options.add_argument("--window-size=1920,1080")
  • chrome_options.add_argument(f'user-agent={userAgent}')

Remove the other arguments:

  • chrome_options.add_argument('--no-sandbox')
  • chrome_options.add_argument('--disable-dev-shm-usage')
  • chrome_options.add_argument("--remote-debugging-port=9222")
  • chrome_options.add_argument('--start-maximized')
  • chrome_options.add_argument('--disable-gpu')
  • chrome_options.add_argument('--disable-popup-blocking')
  • chrome_options.add_argument("--incognito")
  • chrome_options.add_argument('ignore-certificate-errors')

And execute your program as:

  • For Selenium v3.x:

    driver = webdriver.Chrome(executable_path=r'/usr/local/share/chromedriver', options=chrome_options)
    driver.get('http://www.google.com')
    
  • For :

    driver = webdriver.Chrome(service=Service('/usr/local/share/chromedriver'), options=chrome_options)
    driver.get('http://www.google.com')
    
  • Related