selenium icon indicating copy to clipboard operation
selenium copied to clipboard

[🐛 Bug]: Unable to execute selenium in Google Colab

Open yennhi95zz opened this issue 3 years ago • 5 comments

What happened?

I'm trying to execute selenium in Google Colab but unable to succeed. There was an issue and another another. Please refer the code:

!pip install -q selenium
!apt-get update
!apt-get install chromium chromium-driver

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

# Set Chrome options
options = Options()
options.add_argument('--headless') 
options.add_argument("--verbose")
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument("--window-size=1920, 1200")
options.add_argument('--disable-dev-shm-usage')

!which chromedriver
    #output: /usr/bin/chromedriver

# Set path to chromedriver as per your Colab environment
executable_path = '/usr/bin/chromedriver'


service = Service(executable_path=executable_path)
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
driver.get('https://www.google.com/')
driver.quit()

Currently issue:

WARNING:selenium.webdriver.common.selenium_manager:The version of chrome cannot be detected. Trying with latest driver version
---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
[<ipython-input-9-ed0743aa5e5c>](https://localhost:8080/#) in <cell line: 7>()
      5 service = Service(executable_path=executable_path)
      6 options = webdriver.ChromeOptions()
----> 7 driver = webdriver.Chrome(service=service, options=options)
      8 driver.get('https://www.google.com/')
      9 driver.quit()

5 frames
[/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/errorhandler.py](https://localhost:8080/#) in check_response(self, response)
    243                 alert_text = value["alert"].get("text")
    244             raise exception_class(message, screen, stacktrace, alert_text)  # type: ignore[call-arg]  # mypy is not smart enough here
--> 245         raise exception_class(message, screen, stacktrace)

WebDriverException: Message: unknown error: cannot find Chrome binary
Stacktrace:
#0 0x560af168a4e3 <unknown>
#1 0x560af13b9c76 <unknown>
#2 0x560af13e0757 <unknown>
#3 0x560af13df029 <unknown>
#4 0x560af141dccc <unknown>
#5 0x560af141d47f <unknown>
#6 0x560af1414de3 <unknown>
#7 0x560af13ea2dd <unknown>
#8 0x560af13eb34e <unknown>
#9 0x560af164a3e4 <unknown>
#10 0x560af164e3d7 <unknown>
#11 0x560af1658b20 <unknown>
#12 0x560af164f023 <unknown>
#13 0x560af161d1aa <unknown>
#14 0x560af16736b8 <unknown>
#15 0x560af1673847 <unknown>
#16 0x560af1683243 <unknown>
#17 0x7f6de90fc609 start_thread

Also the issue [WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 127](https://stackoverflow.com/questions/49323099/webdriverexception-message-service-chromedriver-unexpectedly-exited-status-co) frequently happens.

Can you suggest a solution without downloading Chrome driver in your local computer?

How can we reproduce the issue?

Suggest a sample code in Google Colab.

Relevant log output

https://www.selenium.dev/documentation/webdriver/troubleshooting/logging/

Operating System

MacOS

Selenium version

Python 3.10.12

What are the browser(s) and version(s) where you see this issue?

Google Colab

What are the browser driver(s) and version(s) where you see this issue?

the built-in driver in Google colab

Are you using Selenium Grid?

4.0.0

yennhi95zz avatar Jul 12 '23 06:07 yennhi95zz

@yennhi95zz, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

github-actions[bot] avatar Jul 12 '23 06:07 github-actions[bot]

Does that code work on your local machine? Is it actually possible to install a browser in Google Collab? Have you checked with them? It all points out to be an environment issue.

diemol avatar Jul 12 '23 07:07 diemol

No more information was given.

diemol avatar Jul 27 '23 05:07 diemol

Hi. This worked for me on Dec 16, 2023. https://www.geeksforgeeks.org/how-to-do-web-scraping-using-selenium-and-google-colab/

haldirleao avatar Dec 16 '23 13:12 haldirleao

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Jan 16 '24 00:01 github-actions[bot]