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 webdriverimport 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:PORTtorexe = os.popen(r'C:\Users\Debanjan.B\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')PROXY = "socks5://localhost:9050" # IP:PORT or HOST:PORToptions = 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:
You can find a relevant discussion in How to connect to Tor browser using Python
Things I'd look at first.
- ports - is the tor socks port interfering with the webdriver ports in any way?
- process order ( can you setup the webdriver and then start the tor service)
- you could setup a selenium hub that connects to selenium nodes through torthis might make the connection between the webdriver, tor, and your test easier to separate.