25

Tor Browser Bundle は Firefox のパッチを適用しただけのバージョンなので、FirefoxDriverTor Browser で使用できるはずです。これは私がこれまでに試したことです:

String torPath = "C:\\Users\\My User\\Desktop\\Tor Browser\\Start Tor Browser.exe";
String profilePath = "C:\\Users\\My User\\Desktop\\Tor Browser\\Data\\Browser\\profile.default";
FirefoxProfile profile = new FirefoxProfile(new File(profilePath));
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
FirefoxDriver driver = new FirefoxDriver(binary, profile);
driver.get("http://www.google.com");

これにより、空白の Tor Browser ページが開き、次のポップアップ メッセージが表示されます: Firefox プロファイルを読み込めません。見つからないか、アクセスできない可能性があります。

ブラウザとプロファイルを正常に起動できるため、プロファイルが有効/互換性があることがわかります。

binary.startProfile(profile, profilePath, ""));

ただし、そのような方法で開いたブラウザにコマンドを送信する方法がわかりません。

同様の質問を見つけましたが、特に Windows でテストされた Java ソリューションを探しています。

ここからダウンロードできるスタンドアロンの Selenium ライブラリと、ここからダウンロードできる Tor Browser Bundle を使用しています

4

5 に答える 5

19

Tor Browser Bundle では WebDriver 拡張機能を使用できなかったので、通常の Firefox ブラウザーから Tor を実行する回避策を見つけました。この方法であれば、Tor Browser が開いていれば、通常の Firefox ブラウザで Tor を使用できます。

  • Torブラウザを開く

    File torProfileDir = new File(
            "...\\Tor Browser\\Data\\Browser\\profile.default");
    FirefoxBinary binary = new FirefoxBinary(new File(
            "...\\Tor Browser\\Start Tor Browser.exe"));
    FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
    torProfile.setPreference("webdriver.load.strategy", "unstable");
    
    try {
        binary.startProfile(torProfile, torProfileDir, "");
    } catch (IOException e) {
        e.printStackTrace();
    }
    
  • いくつかの構成でFirefoxを開きます。

    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);
    FirefoxDriver = new FirefoxDriver(profile);
    
  • ブラウザを閉じます。多くの閉鎖と再開を計画している場合 (新しい IP アドレスを取得するのに役立ちます)、プロファイル設定 toolkit.startup.max_resumed_crashesを のように高い値に設定することをお勧めします9999

    private void killFirefox() {
        Runtime rt = Runtime.getRuntime();
    
        try {
            rt.exec("taskkill /F /IM firefox.exe");
            while (processIsRunning("firefox.exe")) {
                Thread.sleep(100);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private boolean processIsRunning(String process) {
        boolean processIsRunning = false;
        String line;
        try {
            Process proc = Runtime.getRuntime().exec("wmic.exe");
            BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            OutputStreamWriter oStream = new OutputStreamWriter(proc.getOutputStream());
            oStream.write("process where name='" + process + "'");
            oStream.flush();
            oStream.close();
            while ((line = input.readLine()) != null) {
                if (line.toLowerCase().contains("caption")) {
                    processIsRunning = true;
                    break;
                }
            }
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return processIsRunning;
    }
    
于 2014-04-27T07:23:42.540 に答える
1

このコードは、ubuntu でもかなりうまく機能しています。以下に例を示します (JUnit4):

package qa2all;

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

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;


public class HTMLUnit {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {
        //driver = new HtmlUnitDriver();    
        //driver = new FirefoxDriver();
        String torPath = "/home/user/Dropbox/Data/TorBrowser/Linux/32/start-tor-browser";
        String profilePath = "/home/user/Dropbox/Data/TorBrowser/Linux/32/TorBrowser/Data/Browser/profile.default/";
        FirefoxProfile profile = new FirefoxProfile(new File(profilePath));
        FirefoxBinary binary = new FirefoxBinary(new File(torPath));
        driver = new FirefoxDriver(binary, profile);        
        baseUrl = "https://qa2all.wordpress.com";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testUntitled() throws Exception {
        driver.get(baseUrl + "/");

    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private void fail(String verificationErrorString) {
        // TODO Auto-generated method stub

    }
}
于 2015-08-05T08:15:08.787 に答える
0

Tor を使用してブラウザーをリモート制御することに主に関心がある場合 (そして、通常の Firefox の代わりに Tor ブラウザーを使用したい場合)、Mozilla のMarionetteを使用できます。のように使う

Tor ブラウザで使用するには、起動時にマリオネットを有効にします。

Browser/firefox -marionette

(バンドル内)。次に、経由で接続できます

from marionette import Marionette
client = Marionette('localhost', port=2828);
client.start_session()

たとえば、新しいページをロードします

url='http://mozilla.org'
client.navigate(url);

その他の例については、チュートリアルとその他のドキュメントがあります。

コピー

于 2015-09-17T15:14:30.617 に答える