Over a million developers have joined DZone.

Using Selenium WebDriver with Tor C# Code

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

From a really long time, I wanted to write automation using the Tor Web Browser. My preferred automation framework is Selenium WebDriver. However, I found out that there isn’t a built-in integration between the two. I wasted almost half a day to discover how to combine them. Finally, I did it! You can find my discoveries below.

What is Tor?

Tor is free software and an open network that helps you defend against traffic analysis, a form of network surveillance that threatens personal freedom and privacy, confidential business activities and relationships, and state security.

It was originally developed with the U.S. Navy in mind, for the primary purpose of protecting government communications. Today, it is used every day for a wide variety of purposes by normal people, the military, journalists, law enforcement officers, activists, and many others.

As you can see Tor is a modified Firefox browser. You have all features of Firefox plus additional protection.

Download it from here: https://www.torproject.org/

Integrate Tor and Selenium WebDriver

You cannot start the Tor browser using IWebDriver interface because there isn’t an implementation for it. Once started the proxy generated by Tor can be accessed via “ 9051“. So the first step in the plan is to start a new instance of Тоr. Next we start a new FirefoxDriver configured with a new profile with the proxy settings generated by Tor.

public IWebDriver Driver { get; set; }
public Process TorProcess { get; set; }
public WebDriverWait Wait { get; set; }

public void SetupTest()
    String torBinaryPath = @"C:\Users\aangelov\Desktop\Tor Browser\Browser\firefox.exe";
    this.TorProcess = new Process();
    this.TorProcess.StartInfo.FileName = torBinaryPath;
    this.TorProcess.StartInfo.Arguments = "-n";
    this.TorProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;

    FirefoxProfile profile = new FirefoxProfile();
    profile.SetPreference("network.proxy.type", 1);
    profile.SetPreference("network.proxy.socks", "");
    profile.SetPreference("network.proxy.socks_port", 9150);
    this.Driver = new FirefoxDriver(profile);
    this.Wait = new WebDriverWait(this.Driver, TimeSpan.FromSeconds(60));

public void TeardownTest()

By default, Tor is installed on your desktop. We start a new process of the browser which we kill in the test cleanup. The SetPreference method configures the Firefox profile to use the Tor’s proxy settings. You can validate the setup via the test below, just change the IP address with yours. The test will open http://whatismyipaddress.com/ and will verify that your FirefoxDriver is running with different IP.

public void Open_Tor_Browser()
    var expression = By.XPath("//*[@id='section_left']/div[2]");
    this.Wait.Until(x => x.FindElement(expression));
    var element = this.Driver.FindElement(expression);
    Assert.AreNotEqual<string>("", element.Text);

Change Tor Identity via Code

As you know this special browser allows you to change your identity (IP address) via the magic Tor button.

However, it’s possible to do it via telnet or C# code. Use the following guide for telnet: https://stem.torproject.org/faq.html

% telnet localhost 9051
Connected to localhost.
Escape character is '^]'.
250 OK
250 OK

In the following folder “C:\Users\{your_user}\Desktop\Tor Browser\Browser\TorBrowser\Data\Tor“, you are going to find torrc-defaults file (config file). If you want use password authentication, you need to generate a password hash. You can do it via the tor.exe –hash-password “your_password” command.

Then open the torrc-defaults file and add the following line:

hashedControlPassword 16:751C69A9B10D7F4260B04E0D07D7EBCB760EDCEBADD40CDAF40F1FB095

The string after hashedControlPassword is the hash generated for your password.

Your file should be similar to this:

# If non-zero, try to write to disk less frequently than we would otherwise.
AvoidDiskWrites 1
# Where to send logging messages.  Format is minSeverity[-maxSeverity]
# (stderr|stdout|syslog|file FILENAME).
Log notice stdout
# Bind to this address to listen to connections from SOCKS-speaking
# applications.
SocksPort 9150
ControlPort 9151
hashedControlPassword 16:751C69A9B10D7F4260B04E0D07D7EBCB760EDCEBADD40CDAF40F1FB095
CookieAuthentication 0
## fteproxy configuration
ClientTransportPlugin fte exec TorBrowser\Tor\PluggableTransports\fteproxy --managed

## obfsproxy configuration
ClientTransportPlugin obfs2,obfs3,scramblesuit exec TorBrowser\Tor\PluggableTransports\obfsproxy managed

## flash proxy configuration
# Change the second number here (9000) to the number of a port that can
# receive connections from the Internet (the port for which you
# configured port forwarding).
ClientTransportPlugin flashproxy exec TorBrowser\Tor\PluggableTransports\flashproxy-client --register :0 :9000

## meek configuration
ClientTransportPlugin meek exec TorBrowser\Tor\PluggableTransports\terminateprocess-buffer TorBrowser\Tor\PluggableTransports\meek-client-torbrowser --exit-on-stdin-eof -- TorBrowser\Tor\PluggableTransports\meek-client

Now you can use the following C# code to refresh the proxy settings.

public void RefreshTorIdentity()
    Socket server = null;
        IPEndPoint ip = new IPEndPoint(IPAddress.Parse(""), 9151);
        server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        server.Send(Encoding.ASCII.GetBytes("AUTHENTICATE \"johnsmith\"" + Environment.NewLine));
        byte[] data = new byte[1024];
        int receivedDataLength = server.Receive(data);
        string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
        server.Send(Encoding.ASCII.GetBytes("SIGNAL NEWNYM" + Environment.NewLine));
        data = new byte[1024];
        receivedDataLength = server.Receive(data);
        stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
        if (!stringData.Contains("250"))
            Console.WriteLine("Unable to signal new user to server.");

You can find the full source code at the following address: https://github.com/angelovstanton/Projects/tree/master/Demos/TelerikAcademy_WebDriver

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!


Published at DZone with permission of Anton Angelov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}