4

I'm trying to initiate a tor browsing session through Tor Browser 9.5 which uses the default Firefox v68.9.0esr using GeckoDriver and Selenium through Python on a system. But I'm facing an error as:

tor_firefoxESR

Code Block:

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

torexe = os.popen(r'C:\Users\username\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
profile = FirefoxProfile(r'C:\Users\username\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()
firefox_options = webdriver.FirefoxOptions()
firefox_options.binary_location = r'C:\Users\username\Desktop\Tor Browser\Browser\firefox.exe'
driver = webdriver.Firefox(firefox_profile= profile, options = firefox_options, executable_path=r'C:\WebDrivers\geckodriver.exe')
driver.get("https://www.tiktok.com/")

Where as the same code block works through Firefox and Firefox Nightly using the respective binaries.

Do I need any additional settings? Can someone help me out?


Firefox Snapshot:

tor_firefox


Firefox Nightly Snapshot:

tor_firefoxNightly

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

2

I managed to resolve this by updating to v9.5.1 and implementing the following changes:

Note that although the code is in C# the same changes to the Tor browser and how it is launched should be applied.

FirefoxProfile profile = new FirefoxProfile(profilePath);
profile.SetPreference("network.proxy.type", 1);
profile.SetPreference("network.proxy.socks", "127.0.0.1");
profile.SetPreference("network.proxy.socks_port", 9153);
profile.SetPreference("network.proxy.socks_remote_dns", false);

FirefoxDriverService firefoxDriverService = FirefoxDriverService.CreateDefaultService(geckoDriverDirectory);
firefoxDriverService.FirefoxBinaryPath = torPath;
firefoxDriverService.BrowserCommunicationPort = 2828;
var firefoxOptions = new FirefoxOptions
{
    Profile = null,
    LogLevel = FirefoxDriverLogLevel.Trace
};
firefoxOptions.AddArguments("-profile", profilePath);
FirefoxDriver driver = new FirefoxDriver(firefoxDriverService, firefoxOptions);
driver.Navigate().GoToUrl("https://www.google.com");

Important notes:

The following TOR configs need to be changed in about:config :

  • marionette.enabled: true

  • marionette.port: set to an unused port, and set this value to firefoxDriverService.BrowserCommunicationPort in your code. This was set to 2828 in my example.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
numX
  • 830
  • 7
  • 24
  • 1
    I don't think the issue is with **marionette** here. _GeckoDriver_ works through _marionette_ only. Moreover, no ports are occupied on my test machine. – undetected Selenium Jul 09 '20 at 22:07
  • 1
    They weren't on mine either, and yet they didn't work before I explicitly set these configs. Give it a try :) – numX Jul 10 '20 at 12:20
  • Hello, can you send me the Python code? This is only C#? Because I can't find the Module "FirefoxDriverService" in Python. Thanks! – Lowity Jul 30 '20 at 12:19
  • Yes, there is only C# code, I do not know Python well unfortunately. However, the selenium library should provide similar functionality perhaps the classes are named differently – numX Aug 01 '20 at 05:49
1

note:
I am not sure whether this really is the definite answer (thus, I'd really appreciate feedback)

solution:
I've managed to send a get request to the check tor page (https://check.torproject.org/) and it displayed an unknown IP to me (additionally, IPs differ if you repeat the request after a time)

Essentially, I've set up the chrome driver to run TOR. Here's the code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

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(options=chrome_options)


driver.get('https://check.torproject.org/')

Because the driver is not in headless mode you can inspect the resulting page yourself. It should read:
"Congratulations. This browser is configured to use Tor. [IP Info]. However, it does not appear to be Tor Browser. Click here to go to the download page"

Make sure that the chromedriver.exe file is linked on the path or provide the path to the file as an argument to the driver.Chrome() function.

Edit: make sure TOR browser is running in the background, thanks @Abhishek Rai for pointing that out

HRK
  • 51
  • 2
  • You need to have Tor Browser running...beforehand for this to work. – Abhishek Rai Dec 04 '20 at 20:32
  • 1
    Happy to hear it worked & yes, TOR has to run in the background, thanks for pointing out! Do you know if really all traffic is running through TOR in this setup though @AbhishekRai? ... or at least how to properly check for that? – HRK Dec 06 '20 at 09:42
  • I simply set the opening url as wtfismyip.com ..No other reason for it to show my IP as German, I', in India...So, I'm guessing it works. It changes the IP after a while as well. Just doesn't say using a Tor Exit node. – Abhishek Rai Dec 06 '20 at 09:46