I have been getting a series of pesky Selenium/Chrome errors for a week or so, where everything works fine for a while and then suddenly doesn't. I have the right version of Chromedriver (89.0.4389.23) for my version of Google Chrome (89.0.4389.114) per Google Chrome official docs
Currently I'm getting the following error when I run driver = webdriver.Chrome(options=options)
(at least on my production server, I don't get it for the same code repo on my local machine):
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from disconnected: unable to connect to renderer
(Session info: headless chrome=89.0.4389.114)
Frustratingly, the only solution I've found for this error aside from "make sure your chrome and chromedriver match" is to remove options.add_argument("--remote-debugging-port=9222")
from my webdriver options
. I do have that line of code as well, and removing it appears to solve my problem for now, but my problem that line of code appears under the following comments in my codebase:
# adding arg below fixed "DevToolsActivePort file doesn't exist."
# ht https://github.com/heroku/heroku-buildpack-google-chrome/issues/46
options.add_argument("--remote-debugging-port=9222")
So I've now reached a perfect circle where my Python Selenium code reliably works until it reliably breaks, with either of two different Selenium errors ("session not created from disconnected: unable to connect to renderer" or "DevToolsActivePort file doesn't exist."), where the solution to one is to add a specific line of code and the solution to the other is to remove it.
Below is my current launch_webdriver
function, with comments reflecting the various transient Chrome/Selenium errors I've been trying to navigate:
from selenium import webdriver
import chromedriver_binary
from selenium.webdriver.chrome.options import Options
def launch_browser(headless=False): # :, driverpath="./spinocchio/chromedriver"):
options = Options()
# if any other driver `options` are added, they must be added BELOW no-sandbox
# per user parsecer's comment at https://stackoverflow.com/a/53073789/1870832
# and my own excruciating experience.
options.add_argument("--no-sandbox")
# you may think the two lines below can be replaced with just options.headless=headless
# please don't.
if headless:
options.add_argument("headless")
# adding arg below fixed "DevToolsActivePort file doesn't exist."
# ht https://github.com/heroku/heroku-buildpack-google-chrome/issues/46
options.add_argument("--remote-debugging-port=9222")
driver = webdriver.Chrome(options=options)
return driver
How can I get to something more reliable/robust?