Search code examples
python-3.xselenium-webdrivertor

Setting up tor with selenium web driver. (Windows)


i have tried to set up my tor with selenium but it continuously throws up exceptions.

I have tried setting up the binary as well as profiles but no luck.

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

torexe = os.popen(r'C:\Users\Jawad Ahmad Khan\Desktop\Tor Browser\Browser\firefox.exe')
profile = FirefoxProfile(r'C:\Users\Jawad Ahmad Khan\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'D:\geckodriver\geckodriver.exe')
driver.get("http://check.torproject.org")

This is the error message:

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.

Solution

  • This works on my Mac with Chrome with Tor.

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    def get_chrome_webdriver():
    
      tor_proxy = "127.0.0.1:9150"
    
      chrome_options = Options()
      chrome_options.add_argument("--test-type")
      chrome_options.add_argument('--ignore-certificate-errors')
      chrome_options.add_argument('--disable-extensions')
      chrome_options.add_argument('disable-infobars')
      chrome_options.add_argument("--incognito")
      chrome_options.add_argument('--proxy-server=socks5://%s' % tor_proxy)
    
      driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=chrome_options)
    
      return driver
    
    def get_chrome_browser(url):
      browser = get_chrome_webdriver()
      browser.get(url)
    
      return browser
    
    
    get_chrome_browser('https://check.torproject.org/')