jpjacobpadilla / Google-Colab-Selenium

The best way to use Selenium in Google Colab Notebooks!
https://pypi.org/project/google-colab-selenium/
MIT License
156 stars 18 forks source link

Failed to start Chromedriver #3

Closed wanderingkoala closed 6 months ago

wanderingkoala commented 7 months ago

Hey Jacob, I was trying to run the Undetected Chrome driver on your sample colab file. Seems like it's running into a problem. I was hoping you could help fix the issue. Here's the error message I'm getting.

/usr/local/lib/python3.10/dist-packages/undetected_chromedriver/patcher.py in fetch_package(self) 286 logger.debug("downloading from %s" % download_url) --> 287 return urlretrieve(download_url)[0] 288

/usr/lib/python3.10/urllib/request.py in urlretrieve(url, filename, reporthook, data) 240 --> 241 with contextlib.closing(urlopen(url, data)) as fp: 242 headers = fp.info()

/usr/lib/python3.10/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context) 215 opener = _opener --> 216 return opener.open(url, data, timeout) 217

/usr/lib/python3.10/urllib/request.py in open(self, fullurl, data, timeout) 524 meth = getattr(processor, meth_name) --> 525 response = meth(req, response) 526

/usr/lib/python3.10/urllib/request.py in http_response(self, request, response) 633 if not (200 <= code < 300): --> 634 response = self.parent.error( 635 'http', request, response, code, msg, hdrs)

/usr/lib/python3.10/urllib/request.py in error(self, proto, args) 562 args = (dict, 'default', 'http_error_default') + orig_args --> 563 return self._call_chain(args) 564

/usr/lib/python3.10/urllib/request.py in _call_chain(self, chain, kind, meth_name, args) 495 func = getattr(handler, meth_name) --> 496 result = func(args) 497 if result is not None:

/usr/lib/python3.10/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs) 642 def http_error_default(self, req, fp, code, msg, hdrs): --> 643 raise HTTPError(req.full_url, code, msg, hdrs, fp) 644

HTTPError: HTTP Error 404: Not Found

The above exception was the direct cause of the following exception:

StartingChromeDriverError Traceback (most recent call last) in <cell line: 1>() ----> 1 driver = gs.UndetectedChrome() 2 driver.get('https://www.google.com/') 3 print(driver.title) 4 driver.quit()

/usr/local/lib/python3.10/dist-packages/google_colab_selenium/undetected_chromedriver.py in init(self, options, keep_alive) 45 46 except Exception as e: ---> 47 raise StartingChromeDriverError(""" 48 Failed to start ChromeDriver. This could be due to a number 49 of factors, such as missing dependencies or incorrect

StartingChromeDriverError: Failed to start ChromeDriver. This could be due to a number of factors, such as missing dependencies or incorrect configuration settings.

jpjacobpadilla commented 7 months ago

It appears as though the regular version of Selenium works fine and it's just an issue with undetected-chromedriver. There is an issue (and pull request) on the undetected ChromeDriver repository covering the same problem: https://github.com/ultrafunkamsterdam/undetected-chromedriver/issues/1743

For now I'd recommend changing this line of code:

driver = gs.UndetectedChrome()

to:

driver = gs.ChromeDriver()

If you still want to use undetected-chromedriver before this error is resolved, you can monkey patch the problematic function like such:

from google_colab_selenium import UndetectedChromeDriver
from undetected_chromedriver import Patcher
from urllib.request import urlretrieve

def new_fetch_package(self):
    """
    Monkey patch implementing temp fix via this pull request:
    https://github.com/ultrafunkamsterdam/undetected-chromedriver/pull/1745/files#diff-69fd64debbe3fad18a48b33169f3f1c5cd2afe469e6664bde6645838ef13810e
    """
    zip_name = f"chromedriver_{self.platform_name}.zip"
    if self.is_old_chromedriver:
        download_url = "%s/%s/%s" % (self.url_repo, self.version_full.vstring, zip_name)

    else:
        zip_name = zip_name.replace("_", "-", 1)
        download_url = "https://storage.googleapis.com/chrome-for-testing-public/%s/%s/%s"
        download_url %= (self.version_full.vstring, self.platform_name, zip_name)

    return urlretrieve(download_url)[0]

Patcher.fetch_package = new_fetch_package
driver = UndetectedChromeDriver()

driver.get('https://www.google.com')
print(driver.title)
driver.quit()
jpjacobpadilla commented 6 months ago

undetected-chromedriver problem resolved.