Eigi
Eigi

Reputation: 716

Java and Tor Browser with Selenium

I have searched the web for a solution for hours, but they are all outdated or not working.

Does anyone have a working solution to get a connection with Java + Selenium + Tor Browser?

A short example would be enough.

My best try, with a "tor failed to start" error:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;

import java.io.File;
import java.util.concurrent.TimeUnit;

public class JSelenium {

public static void main(String[] args) {
    System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
    System.setProperty("webdriver.firefox.marionette", "geckodriver.exe");
    FirefoxOptions options = new FirefoxOptions();
    FirefoxProfile torProfile = new FirefoxProfile(new File("xxx\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default"));

    options.setBinary("xxx\\Tor Browser\\Browser\\firefox.exe");
    options.setProfile(torProfile);
    options.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options);

    WebDriver driver = new FirefoxDriver(options);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.navigate().to("http://www.google.com");
}

}

Upvotes: 1

Views: 1152

Answers (3)

Lakshan Enosh
Lakshan Enosh

Reputation: 219

I also faced the same issue. The following settings worked well for me:

FirefoxProfile profile = new FirefoxProfile(torProfileDir);
profile.setPreference("extensions.torlauncher.start_tor", false);

Upvotes: 0

Eigi
Eigi

Reputation: 716

Worked with following parameters (I guess the control port is important and some extension options):

    profile.setPreference("network.proxy.type", 1);
    profile.setPreference("network.proxy.socks", "127.0.0.1");
    profile.setPreference("extensions.torlauncher.start_tor", false);
    profile.setPreference("extensions.torbutton.block_disk", false);
    profile.setPreference("extensions.torbutton.custom.socks_host", "127.0.0.1");
    profile.setPreference("extensions.torbutton.custom.socks_port", 9150);
    profile.setPreference("extensions.torbutton.inserted_button", true);
    profile.setPreference("extensions.torbutton.launch_warning", false);
    profile.setPreference("privacy.spoof_english", 2);
    profile.setPreference("extensions.torbutton.loglevel", 2);
    profile.setPreference("extensions.torbutton.logmethod", 0);
    profile.setPreference("extensions.torbutton.settings_method", "custom");
    profile.setPreference("extensions.torbutton.use_privoxy", false);
    profile.setPreference("extensions.torlauncher.control_port", 9251); // 9251
    profile.setPreference("extensions.torlauncher.loglevel", 2);
    profile.setPreference("extensions.torlauncher.logmethod", 0);
    profile.setPreference("extensions.torlauncher.prompt_at_startup", false);
    profile.setPreference("browser.startup.page", "0");
    profile.setPreference("browser.startup.homepage", "about:newtab");
    profile.setPreference("extensions.torlauncher.prompt_at_startup", 0);
    profile.setPreference("webdriver.load.strategy", "normal");
    profile.setPreference("app.update.enabled", false);
    profile.setPreference("extensions.torbutton.versioncheck_enabled", false);
    profile.setPreference("extensions.torbutton.prompted_language", true);
    profile.setPreference("network.proxy.socks_port", 9150);
    profile.setPreference("extensions.torbutton.socks_port", 9150);

Upvotes: 1

Walerius
Walerius

Reputation: 19

FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("network.proxy.type", 1);
    profile.setPreference("network.proxy.socks", "127.0.0.1");
    profile.setPreference("network.proxy.socks_port", 9150);

    options.setProfile(profile);

Add this to your Java Code and open a new Firefox Browser. The importent part is you need to run tor.exe once. You can find it in the Tor folder under "Tor Browser\Browser\TorBrowser\Tor".

Upvotes: 1

Related Questions