2

With the following code, on a Mac I tried to start Tor browser with Python and Selenium:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
import os
import time

binary = '/Applications/Tor Browser.app/Contents/MacOS/firefox'

# binary location
if os.path.exists(binary) is False:
    raise ValueError("The binary path to Tor firefox does not exist.")
firefox_binary = FirefoxBinary(binary)

browser = None

def get_browser(binary=None, options=None):
    global browser
    # only one instance of a browser opens
    if not browser:
        browser = webdriver.Firefox(firefox_binary=binary, options=options)
    return browser

browser = get_browser(binary=firefox_binary)

time.sleep(20)
browser.get("http://stackoverflow.com")
time.sleep(10)
html = browser.page_source
print(html)

This actually WORKS, but I get the following warning:

DeprecationWarning: firefox_binary has been deprecated, please pass in a Service object
  browser = webdriver.Firefox(firefox_binary=binary,options=options)

I searched for a way to pass this Service object, but nothing worked: substantially, I tried to pass the object the way other browsers do.

In fact, while other browsers have a documented Service class, even if I can import without any error from selenium.webdriver.firefox.service a Service class, the webdriver constructor doesn't feature any service object or it is not documented.

Any help is appreciated.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Life after Guest
  • 299
  • 1
  • 11
  • 1
    it is only warning so stop bother this. And as I know `Tor` uses little older `Firefox` - my `tor` on Linux uses Firefox `91.7` (and it is says it is `updated`), but normal Firefox has number `98.0` - so maybe `Service` doesn't work with older `Firefox` – furas Mar 12 '22 at 19:01

1 Answers1

3

This error message...

DeprecationWarning: firefox_binary has been deprecated, please pass in a Service object
  browser = webdriver.Firefox(firefox_binary=binary,options=options)

...implies that the argument firefox_binary is deprecated now and you need to pass a Service object instead.


Details

This error is inline with the current implementation of webdriver as per webdriver.py

if firefox_binary:
    warnings.warn('firefox_binary has been deprecated, please pass in a Service object',
                  DeprecationWarning, stacklevel=2)

Solution

As per Selenium v4.0 Beta 1:

  • Deprecate all but Options and Service arguments in driver instantiation. (#9125,#9128)

So instead of firefox_binary you have to use the binary_location property and pass it through an instance of FirefoxOptions() as follows:

from selenium import webdriver
from selenium.webdriver.firefox.service import Service

option = webdriver.FirefoxOptions()
option.binary_location = r'/Applications/Tor Browser.app/Contents/MacOS/firefox'
driverService = Service('/path/to/geckodriver')
driver = webdriver.Firefox(service=driverService, options=option)
driver.get("https://www.google.com")

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 2
    Hey, I was about to vote it! :-) Anyway, I must say one thing: I tested your code, and I found it just works without any Service: the option.binary_location part is enough for the warning to disappear – Life after Guest Mar 13 '22 at 07:50
  • Update to the previous comment: by not instantiating the Service, custom torrc file settings are ignored. I purposedly write it down in case someone comes here in the future :-) – Life after Guest Mar 13 '22 at 08:14