0

I try to open a Tor Browser using Selenium on Ubuntu 18. I have tried lots of examples but with no success.

proxyIP = "127.0.0.1"
proxyPort = "9050"
profileTor = '/etc/tor/' # torrc
binary = os.path.expanduser("~/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US/Browser/firefox")

firefox_binary = FirefoxBinary(binary)
firefox_profile = FirefoxProfile(profileTor)
proxy_address = "127.0.0.1:9050"
proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': proxy_address,
 })
driver = webdriver.Firefox(firefox_binary = firefox_binary,firefox_profile=firefox_profile, proxy = proxy)

A blank Tor Browser window opens but after a while I get an error as:

selenium.common.exceptions.WebDriverException: Message: connection refused.

I have also tried an alternative to firefox binary the:

start-tor-browser

which opens a working Tor Browser and showing some index. The script however stops and I cannot visit another page using Selenium unless I do it manually.

I have also tried the:

profile.default

as some examples suggest but I get an error:

Unable to start Tor. The torrc file is missing and could not be created.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • The only workaround that I have found is this post: https://stackoverflow.com/a/21836296/3816638 It would probably work on all .onion links. – user3816638 Jul 23 '18 at 13:58
  • See also my post here, it might be helpful: https://stackoverflow.com/a/53536691/3816638 Don't forget to check the port of the tor network. – user3816638 Dec 11 '18 at 16:16

1 Answers1

0

To open a Tor Browser using Selenium you can start the Tor daemon first and then open the Tor Browser and you can use the following solution:

  • Sample WindowsOS style Code Block:

    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