How to connect to Tor browser using Python
Asked Answered
A

3

5

I am trying to connect to a Tor browser but get an error stating "proxyConnectFailure" any ideas I have tried multiple attempts to get into the basics of Tor browser to get it connected but all in vain if any could help life could be saved big time:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary


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

# Configured profile settings.

proxyIP = "127.0.0.1"
proxyPort = 9150

proxy_settings = {"network.proxy.type":1,
    "network.proxy.socks": proxyIP,
    "network.proxy.socks_port": proxyPort,
    "network.proxy.socks_remote_dns": True,
}
driver = webdriver.Firefox(firefox_binary=binary,proxy=proxy_settings)

def interactWithSite(driver):

    driver.get("https://www.google.com")    
    driver.save_screenshot("screenshot.png")

interactWithSite(driver)
Abnormity answered 27/12, 2018 at 9:16 Comment(1)
Possible duplicate of Running Selenium Webdriver with a proxy in PythonDiurnal
H
13

To connect to a Tor Browser through a FirefoxProfile you can use the following solution:

  • 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")
    
  • Browser Snapshot:

Tor_Python


You can find a relevant discussion in How to use Tor with Chrome browser through Selenium

Hesperidin answered 27/12, 2018 at 9:35 Comment(14)
still the same error actually i am connecting to tor browser but not able to visit the websiteAbnormity
Can you crosscheck first if you are able to connect to the url http://check.torproject.org and see the message This browser is configured to use Tor. first?Hesperidin
So basically you aren't connected via Tor. Did you try to use the code block from the answer and gave it a try as it is?Hesperidin
yes i did so basically it opens up the tor window when i manually enter check.torproject.org it opens up but not with codeAbnormity
Nopes, you should be able to invoke the url http://check.torproject.org without any issue which should confirm you are invoking Tor correctly. That's the basic step.Hesperidin
selenium.common.exceptions.WebDriverException: Message: Reached error page: about:neterror?e=proxyConnectFailure&u=https%3A//check.torproject.org/&c=UTF-8&f=regular&d=Firefox%20is%20configured%20to%20use%20a%20proxy%20server%20that%20is%20refusing%20connections. this is the error that i getAbnormity
dns is not resolving the url. Simply copy and paste the code from the answer and retest.Hesperidin
Finally connected i had to change the sock_port to 9150 and that did the trickAbnormity
can you help me in more sceanrioAbnormity
Feel free to raise a new question with your new requirement. StackOverflow contributors will be happy to help you out.Hesperidin
how to close the current tor browser and reopen it with a new linkAbnormity
@DebanjanB by changing the sock_port to 9150 you can only reach the website that is not censored. see this https://mcmap.net/q/67434/-using-the-firefox-profile-and-the-tor-daemon-doesn-39-t-work/11750176 can you help?Centra
As of 2022 this is not working anymoreUstulation
I able to open websites with this code. But .onion website is not reachable with this codeHacking
K
2

I would like to expand on @DebanjanB answer by adding the Linux counterpart:

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

torexe = os.popen('some/path/tor-browser_en-US/Browser/start-tor-browser') 
# in my case, I installed it under a folder tor-browser_en-US after
# downloading and extracting it from 
# https://www.torproject.org/download/ for linux

profile = FirefoxProfile(
    'some/path/tor-browser_en-US/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()

firefox_options = webdriver.FirefoxOptions()
firefox_options.binary_location = '/usr/bin/firefox' 
# /usr/bin/firefox is default location of firefox - for me anyway

driver = webdriver.Firefox(
    firefox_profile=profile, options=firefox_options, 
    executable_path='wherever/you/installed/geckodriver') 
# I keep my geckodriver(s) in a special folder sorted by versions.
# Geckodriver downloadable here: 
# https://github.com/mozilla/geckodriver/releases/
driver.get("http://check.torproject.org")
Kursh answered 12/4, 2021 at 18:22 Comment(1)
As of 2022 this is not working anymoreUstulation
B
1

The verified answer does not work in case of opening dot onion sites(I believe that's something to do with tor network which is not allowing access to normal firefox).

As for the latest tor browser (from the tor browser bundle), starting it using selenium causes some error due to which the browser cannot start tor proxy itself causing proxy and timeout errors(doesn't matter if tor proxy is started by python or manually or not started at all). This could also be due to port 9050 or 9150 being used by tor proxy and not being available to browser's tor instance but this does not explain the error caused when no instance of tor proxy is running.

The solution i have found is to start the tor proxy as normal, manually or using os.popen("tor.exe") and configure tor browser to not start tor proxy. here's the code:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

os.popen(r'e:\\bla\\bla\\bla\\tor\\Tor\\tor.exe')

binary=FirefoxBinary(r'e:\\bla\\bla\\bla\\Tor Browser\\Browser\\firefox.exe')
fp=FirefoxProfile(r'e:\\foo\\bar\\bla\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default')

fp.set_preference('extensions.torlauncher.start_tor',False)#note this
fp.set_preference('network.proxy.type',1)
fp.set_preference('network.proxy.socks', '127.0.0.1')
fp.set_preference('network.proxy.socks_port', 9050)
fp.set_preference("network.proxy.socks_remote_dns", True)
fp.update_preferences()

driver = webdriver.Firefox(firefox_profile=fp,firefox_binary=binary)
driver.get("http://check.torproject.org")
driver.get('https://www.bbcnewsv2vjtpsuy.onion/')

*note fp.set_preference('extensions.torlauncher.start_tor',False) on line 10 is being used to configure tor to not start its own tor instance so that it uses the proxy config and tor instance started above.

lo and behold as the tbb starts working like normal firefox bot browser

Buber answered 12/9, 2021 at 23:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.