тарас дідик
тарас дідик

Reputation: 66

Java Selenium open TOR browser

I try to open the Tor browser by Java + Selenium + CheckoDriver + Tor browser. The code below open the Tor browser but with the "Tor failed to start" error. Maybe someone faced with this problem or have some another solution to solve problem (only on the Java) thanks !

 public static void test3() {
                System.setProperty("webdriver.gecko.driver", "C:\\tor\\geckodriver.exe");
                System.setProperty("webdriver.firefox.marionette", "C:\\tor\\geckodriver.exe");

                FirefoxOptions options = new FirefoxOptions();
                FirefoxProfile torProfile = new FirefoxProfile(new File("C:\\tor\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default"));
                torProfile.setPreference("network.proxy.type", 1);
                torProfile.setPreference("network.proxy.socks", "127.0.0.1");
                torProfile.setPreference("network.proxy.socks_port", 9150);
                options.setBinary("C:\\tor\\Tor Browser\\Browser\\firefox.exe");
                options.setProfile(torProfile);
                options.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options);

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

I think, maybe it is problem with setPreference ( proxy.. )

Upvotes: 2

Views: 587

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193098

To open the TOR browser by using Selenium, GheckoDriver and Java, you need to start the daemon first through Runtime.getRuntime().exec() and you can use the following solution:

package demo;

import java.io.File;
import java.io.IOException;

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

public class A_Tor_Firefox {

    public static void main(String[] args) throws IOException {


    Runtime.getRuntime().exec("C:/Users/Debanjan.B/Desktop/Tor Browser/Browser/TorBrowser/Tor/tor.exe");
    System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
    FirefoxOptions options = new FirefoxOptions();
    options.setBinary("C:\\Users\\Debanjan.B\\Desktop\\Tor Browser\\Browser\\firefox.exe");
    FirefoxProfile profile = new FirefoxProfile(new File("C:\\Users\\Debanjan.B\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default"));
    profile.setPreference("network.proxy.type", 1);
    profile.setPreference("network.proxy.socks", "127.0.0.1");
    profile.setPreference("network.proxy.socks_port", 9150);
    profile.setPreference("network.proxy.socks_remote_dns", "False");
    options.setProfile(profile);
    WebDriver driver = new FirefoxDriver(options);
    driver.get("http://check.torproject.org");

    }
}

Upvotes: 1

Related Questions