How to Connect to Tor Browser Using Python

How to connect to Tor browser using Python

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

Make requests using Python over Tor

Here is the code you want to use (download the stem package using pip install stem)

from stem import Signal
from stem.control import Controller

with Controller.from_port(port = 9051) as controller:
controller.authenticate(password='your password set for tor controller port in torrc')
print("Success!")
controller.signal(Signal.NEWNYM)
print("New Tor connection processed")

Good luck and hopefully that works.

How to use Tor with Chrome browser through Selenium

To use Tor with Chrome browser through Selenium you can use the following solution:

  • Code Block:

    from selenium import webdriver
    import os

    # To use Tor's SOCKS proxy server with chrome, include the socks protocol in the scheme with the --proxy-server option
    # PROXY = "socks5://127.0.0.1:9150" # IP:PORT or HOST:PORT

    torexe = os.popen(r'C:\Users\Debanjan.B\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
    PROXY = "socks5://localhost:9050" # IP:PORT or HOST:PORT
    options = webdriver.ChromeOptions()
    options.add_argument('--proxy-server=%s' % PROXY)
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("http://check.torproject.org")
  • Browser Snapshot:

Tor_Chrome


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

How to click a button in Tor Browser with Selenium and python

I'm assuming you are trying to bypass a CAPTCHA.

You can do this one of two ways. You can click the button by using a selector. For example, an XPath selector for a button with class "g-recpatcha". You can also just execute JavaScript code on the page to call the onSubmit() function.

So two options are:

  1. driver.find_element_by_xpath("//button[@class='g-recaptcha']").click()

  2. driver.execute_script("onSubmit("" + captchaToken + "")")

See the reCAPTCHA callback on 2captcha API, Solving Captchas.



Related Topics



Leave a reply



Submit