2

I was looking at this excellent answer and copied the code for MacOS (top of answer, also reproduced below for convenience).

import os    # Code from answer linked above
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium import webdriver


# path to the firefox binary inside the Tor package
binary = '/Applications/TorBrowser.app/Contents/MacOS/firefox'
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):
    global browser  
    # only one instance of a browser opens, remove global for multiple instances
    if not browser: 
        browser = webdriver.Firefox(firefox_binary=binary)
    return browser

if __name__ == "__main__":
    browser = get_browser(binary=firefox_binary)
    urls = (
        ('tor browser check', 'https://check.torproject.org/'),
        ('ip checker', 'http://icanhazip.com')
    )
    for url_name, url in urls:
        print "getting", url_name, "at", url
        browser.get(url)

I keep getting the following error.

Traceback (most recent call last):
  File "torselenium.py", line 22, in <module>
    browser = get_browser(binary=firefox_binary)
  File "torselenium.py", line 18, in get_browser
    browser = webdriver.Firefox(firefox_binary=binary)
  File "/Users/user/Library/Python/2.7/lib/python/site-packages/selenium/webdriver/firefox/webdriver.py", line 164, in __init__
    self.service.start()
  File "/Users/user/Library/Python/2.7/lib/python/site-packages/selenium/webdriver/common/service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 

This error is very similar to the error described in this question, where the recomendation is to set a firefox_binary path. Yet I have already done this and verified (verification proof below) that there is a firefox binary at that path.

$ ls /Applications/TorBrowser.app/Contents/MacOS/firefox
/Applications/TorBrowser.app/Contents/MacOS/firefox

In fact, if I set the binary path (line 7) to '~/Applications/TorBrowser.app/Contents/MacOS/firefox', it gives me the ValueError, so there is definitely a binary there.

I also checked if I have execute permissions, which I do and I tried changing browser from a global to local variable, to no help.

How can I resolve this error?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • `firefox` and `geckodriver` are two different executables. What does `command -v geckodriver` return? If not installed, `brew install geckodriver` – hoefling Jun 28 '20 at 20:27
  • @hoefling `brew install geckodriver` solved my problem. Not sure why, as I have used selenium in the past and I don't think I had it installed using brew - just downloaded the executable from Mozilla –  Jun 28 '20 at 21:14
  • You can install it however you want, downloading the executable from [geckodriver releases](https://github.com/mozilla/geckodriver/releases) is also fine, just make sure the file is in PATH. – hoefling Jun 28 '20 at 21:26

2 Answers2

1

This works for me

from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile 
    
driver = webdriver.Firefox(
                executable_path=GeckoDriverManager().install(),
                firefox_profile= FirefoxProfile(R"C:\Users\Usuario\Desktop\TorBrowser\Browser\TorBrowser\Data\Browser\profile.default"),
                firefox_binary=FirefoxBinary(R'C:\Users\Usuario\Desktop\TorBrowser\Browser\firefox.exe')
            )
driver.get("https://google.com")

i have to use binary, profile an GeckoDriverManager library

Jbryan Reyes
  • 31
  • 1
  • 6
0

Seems you were close enough. Using to initiate a Mozilla Firefox browsing context through Selenium driven GeckoDriver you have to download the latest GeckoDriver v0.26.0 and store it in your system and mention the absolute path while initiating the Web Browser session as follows :

browser = webdriver.Firefox(firefox_binary=binary, executable_path='/usr/local/bin/geckodriver')

You can find a detailed discussion in How to connect to Tor browser using Python


References

You can find a couple of relevant discussions on WebDriverException: 'geckodriver' executable needs to be in PATH error in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352