3
votes

Im currently trying to execute TOR 6.0.4 with Selenium WebDriver (JAVA) 2.53 and Firefox 43.0. I've followed the instructions from this post Using Selenium WebDriver with Tor but Im getting an error while loading the profilePath of TOR to the Firefox Binary. I've seen that is possible to lunch TOR by loading the TOR profile.default archive to the firefox binaty, but Im getting a Driver info: driver.version: unknown, when instantiating the binary with the profile. I've tried to change the firefox version and still. Below the code where I start the driver. Im also using Windows.

 String torPath = "C:\\Users\\Jose Bernhardt\\Desktop\\Tor Browser\\Start Tor Browser.exe";
    String profilePath = "C:\\Users\\Jose Bernhardt\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";

    File torProfileDir = new File(profilePath);
    FirefoxBinary binary = new FirefoxBinary(new File(torPath));
    FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
    FirefoxDriver driver = new FirefoxDriver();
    driver.get("http://www.google.com/webhp?complete=1&hl=en");

See below the exception thrown:

Exception in thread "main" org.openqa.selenium.WebDriverException: Specified firefox binary location does not exist or is not a real file: C:\Users\Jose Bernhardt\Desktop\Tor Browser\Start Tor Browser.exe
3

3 Answers

3
votes

Seems that I was loading the Tor.exe and instead I had to load the firefox.exe from the Tor archive. I change my path to this and is working. Also fix that I was not sending the profile and the binary to the driver constructor

 "C:\\Users\\Jose Bernhardt\\Desktop\\Tor Browser\\Browser\\firefox.exe"
FirefoxDriver driver = new FirefoxDriver(binary, torProfile);
2
votes
System.setProperty("webdriver.firefox.marionette", ".\\geckodriver.exe");
        String torPath = "C:\\Users\\HP\\Desktop\\Tor Browser\\Browser\\firefox.exe";
        String profilePath = "C:\\Users\\HP\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";

        File torProfileDir = new File(profilePath);
        FirefoxBinary binary = new FirefoxBinary(new File(torPath));
        FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);

        FirefoxOptions options = new FirefoxOptions();
        options.setBinary(binary);
        options.setProfile(torProfile);
        options.setCapability(FirefoxOptions.FIREFOX_OPTIONS,options);
        WebDriver driver = new FirefoxDriver(options);
0
votes

Till now Tor browser is supported using the Mozilla Firefox Here is the code below:

System.setProperty("webdriver.gecko.driver", "C:\\Users\\user\\eclipse-workspace\\TorSelenium\\src\\main\\resources\\geckodriver.exe");//sa used for the firefox
            String torBinaryPath = "C:\\Users\\user\\OneDrive\\Desktop\\Lk's stuff\\Tor Browser\\Browser\\firefox.exe"; //It is inside the tor browser's folder
            
            
            
            Runtime runTime = Runtime.getRuntime();
            Process torProcess = runTime.exec(torBinaryPath + " -n");
            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);
            FirefoxOptions firefoxOptions = new FirefoxOptions();
            firefoxOptions.setProfile(profile);
            WebDriver driver;
            driver = new FirefoxDriver(firefoxOptions);
            driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
            driver.manage().window().maximize();
            WebDriverWait wait;
            wait = new WebDriverWait(driver, 30);
            JavascriptExecutor js = (JavascriptExecutor) driver;
            //added end
            
            
            
            System.out.println(ean);
            //Thread.sleep(100);
            //driver.navigate().to("https://www.google.com/?hl=en");
            //driver.navigate().to("https://duckduckgo.com/?q=d&ia=web");
            driver.navigate().to("https://www.swiggy.com");

If you want to change the tor identity then you have to restart the tor using and again start above code:

Runtime.getRuntime().exec("taskkill /f /IM firefox");
            Runtime.getRuntime().exec("taskkill /f /IM firefox.exe");
            if(torProcess.isAlive()) {
                System.out.println("destroying tor");
                torProcess.destroy();
            }
            if(torProcess.isAlive()) {
                System.out.println("forcebly destroying tor");
                torProcess.destroyForcibly();
            }