Kanarsky
Kanarsky

Reputation: 172

Using Selenium WebDriver + Tor as proxy

I try to connect to a specific site using Selenium WebDriver Firefox through TOR Socks5 at 9050 port and I can't establish the connection.

profile = FirefoxProfile()    
profile.set_preference('network.proxy.type', 1)
profile.set_preference( "network.proxy.socks_version", 5 )
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", True )    
browser = webdriver.Firefox(firefox_profile=profile)

The site is probably blocking some TOR connections but the odd thing is that I can connect to it using TorBrowser! I even found the exit node that was used by TorBrowser and edited my torrc file to use it too (ExitNodes 'ip'). I checked that my selenium Firefox's exit node was the same (I can successfuly connect to other sites through TOR proxy and check my ip), but I still can't connect, even using the same ip! Where is my mistake?

And the second thing is that if I set up:

profile.set_preference('network.proxy.socks_port', 9150) 

i.e. use TorBrowser proxy, selenium Firefox successfully establishes connection to the site .

Is it something wrong with my tor settings?

Upvotes: 3

Views: 3272

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193058

To connect to a specific site using Selenium WebDriver, GeckoDriver and Firefox through TOR Socks5 on port 9050 you can start the tor daemon and using a FirefoxProfile 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")

Upvotes: 1

Related Questions