0

I am trying to use Selenium in Google Colab, but I get some errors when I try to run a Firefox instance. i followed this links:

  • Selenium Documentation here, I tried with the Driver Management Software but I got the error that says that was unable to find the binary location to Firefox. So I tried with the Hard Coded Location method, but I still have the same error, so I think that I must install firefox in google colab, but I don't know if this is a correct approach

Here is my code:

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager
from google.colab import drive

drive.mount('/content/drive', force_remount=True)

service = FirefoxService(executable_path= '/content/drive/MyDrive/geckodriver')
driver = webdriver.Firefox(service=service)

And this is the entire error:

SessionNotCreatedException                Traceback (most recent call last)

<ipython-input-7-51ea1584f592> in <module>()
      1 service = FirefoxService(executable_path= '/content/drive/MyDrive/geckodriver')
----> 2 driver = webdriver.Firefox(service=service)

4 frames

/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    245                 alert_text = value['alert'].get('text')
    246             raise exception_class(message, screen, stacktrace, alert_text)  # type: ignore[call-arg]  # mypy is not smart enough here
--> 247         raise exception_class(message, screen, stacktrace)
    248 
    249     def _value_or_default(self, obj: Mapping[_KT, _VT], key: _KT, default: _VT) -> _VT:

SessionNotCreatedException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line

Edit

i tried installing firefox on Google colab, and i think all is correct, but when it tried to run firefox i get this output on geckodriver.log:

1643070515477   geckodriver INFO    Listening on 127.0.0.1:43355
1643070515979   mozrunner::runner   INFO    Running command: "/usr/bin/firefox" "--marionette" "--headless" "--no-sandbox" "--disable-dev-shm-usage" "--remote-debugging-port" "43945" "-no-remote" "-profile" "/tmp/rust_mozprofileeDGKcf"
src/tcmalloc.cc:283] Attempt to free invalid pointer 0x7fe67e416250 
Redirecting call to abort() to mozalloc_abort

Here is the code:

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager
from google.colab import drive

drive.mount('/content/drive', force_remount=True)
service = FirefoxService(executable_path= '/content/drive/MyDrive/geckodriver')
options = webdriver.FirefoxOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Firefox(service= service, options= options)

Ernesto
  • 129
  • 2
  • 10
  • wasn't this addressed here https://stackoverflow.com/questions/70836496/filenotfounderror-errno-2-no-such-file-or-directory-content-drive-mydrive? – cruisepandey Jan 24 '22 at 17:43
  • @cruisepandey, that was about another error i got but this is another error so i think its better to be in another question. – Ernesto Jan 24 '22 at 18:18
  • sounds like you need to set the path to Firefox. (the browser, not the driver) Maybe see this thread: https://stackoverflow.com/questions/58296262/python-selenium-4-firefox-firefoxbinary-deprecated – pcalkins Jan 24 '22 at 21:27
  • @pcalkins yes i need, but the question is if i have to install firefox in google colab, or there is a better solution. – Ernesto Jan 24 '22 at 21:31
  • You'll need it installed on the machine that runs the webdriver, OR installed on a machine that runs a remote end/grid. – pcalkins Jan 24 '22 at 21:41

1 Answers1

0

Seems you are downloading and storing the GeckoDriver in a network location and then mounting it:

drive.mount('/content/drive', force_remount=True)

As per several discussions, GeckoDriver / FirefoxService can't be initiated from a network location.

You can find a couple of relevant discussions in:


Solution

So a generalized solution would be to access GeckoDriver / Firefox by putting them into the local drives of the host machine.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • i tried installing firefox on google colab, but i cant make it run, i got this output on geckodriver.log ``` 1643070515477 geckodriver INFO Listening on 127.0.0.1:43355 1643070515979 mozrunner::runner INFO Running command: "/usr/bin/firefox" "--marionette" "--headless" "--no-sandbox" "--disable-dev-shm-usage" "--remote-debugging-port" "43945" "-no-remote" "-profile" "/tmp/rust_mozprofileeDGKcf" src/tcmalloc.cc:283] Attempt to free invalid pointer 0x7fe67e416250 Redirecting call to abort() to mozalloc_abort ``` i searched, but i get stuck here. – Ernesto Jan 25 '22 at 00:37