8

I've tried a multitude of solutions to this, but so far haven't had any luck. I'm trying to access the tor browser using selenium in python, but when my program opens up Tor, Tor gives me an error message saying:

Tor failed to start.  

Python then gives the following error message:

selenium.common.exceptions.WebDriverException: Message: permission denied

My code is the following:

binary = FirefoxBinary(r"C:\\Users\\User\\Desktop\\Tor Browser\\Browser\\firefox.exe")
profile = FirefoxProfile(r"C:\\Users\\User\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default")

driver = webdriver.Firefox(firefox_binary=binary)
driver = webdriver.Firefox(firefox_profile= profile, firefox_binary= binary, executable_path = r"C:\\Users\\User\\Desktop\\geckodriver.exe")
driver.profile.set_preference('network.proxy.type', 1)
driver.profile.set_preference('network.proxy.socks', '127.0.0.1')
driver.profile.set_preference('network.proxy.socks_port', 9150)
profile.set_preference("network.proxy.socks_remote_dns", False)
profile.update_preferences()
driver.get("http://yahoo.com")

Any help on this would be greatly appreciated!

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

1 Answers1

2

To access the Tor browser using Selenium through Python you can use the following solution:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
import os

torexe = os.popen(r'C:\Users\AtechM_03\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
profile = FirefoxProfile(r'C:\Users\AtechM_03\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default')
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9050)
profile.set_preference("network.proxy.socks_remote_dns", False)
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile= profile, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("http://check.torproject.org")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Thanks for the help! :) For me the code worked without `popen()` as well. Is it necessary? What is it for? – Charalamm Sep 02 '20 at 08:55
  • @Phineas This answer is almost a year and half old. I have to recheck and come back to you. Your feedback really helps. See: [How to connect to Tor browser using Python](https://stackoverflow.com/questions/53942553/how-to-connect-to-tor-browser-using-python/53942783#53942783) and [How to initiate a Tor Browser 9.5 which uses the default Firefox to 68.9.0esr using GeckoDriver and Selenium through Python](https://stackoverflow.com/questions/62666075/how-to-initiate-a-tor-browser-9-5-which-uses-the-default-firefox-to-68-9-0esr-us) – undetected Selenium Sep 02 '20 at 09:00
  • It works as well without the proxy configurations. Why are they needed? – Charalamm Sep 02 '20 at 09:03
  • 1
    It seems like the proxy configurations are for actually using the proxy. Without them the [checking tor service](https://check.torproject.org/) shows `Sorry. You are not using Tor.` – Charalamm Sep 02 '20 at 09:12