使用Selenium WebDriver和Tor [英] Using Selenium WebDriver with Tor

查看:1001
本文介绍了使用Selenium WebDriver和Tor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Tor Browser Bundle只是Firefox的一个补丁版本,看起来应该可以在Tor Browser上使用 FirefoxDriver 。这是我迄今为止所尝试的:

 字符串torPath =C:\\ Users \\ My用户\\\桌面\\\\\\\\\\\\\\\\\\\\\\\\' 
String profilePath =C:\\Users\\\\ My User \\\\\ Desktops\\Tor Browser\\\\\\\\\\\\\\\\\' ;
FirefoxProfile profile = new FirefoxProfile(new File(profilePath));
FirefoxBinary二进制=新的FirefoxBinary(新文件(torPath));
FirefoxDriver driver = new FirefoxDriver(binary,profile);
driver.get(http://www.google.com);

这会导致一个空白的Tor浏览器页面打开一个弹出消息:您的Firefox配置文件不能被加载。它可能会丢失或无法访问。



我知道该配置文件是有效的/兼容的,因为我可以成功地启动浏览器和配置文件:

  binary.startProfile(profile,profilePath,)); 

但是,我不知道如何将命令发送到以这种方式打开的浏览器。 / b>

我发现了类似的问题,但是我特别寻找一个Java解决方案,最好在Windows上测试。



我正在使用一个独立的Selenium库,可以在这里下载 和Tor Browser Bundle请此处

解决方案

由于Tor浏览器套件不让我使用WebDriver扩展,所以我找到了一个解决方法,我从常规的Firefox浏览器中运行Tor。使用这种方法,只要Tor浏览器打开,您就可以使用常规的Firefox浏览器使用Tor。


  • 打开Tor浏览器

     文件torProfileDir =新文件(
    ... \\\ \\浏览器\\ \\ \\数据\\ \\ \\浏览器\\ \\ \\ \\ \\ profile \\默认);
    FirefoxBinary binary = new FirefoxBinary(new File(
    ... \\Tor Browser\\Start Tor Browser.exe));
    FirefoxProfile torProfile =新的FirefoxProfile(torProfileDir);
    torProfile.setPreference(webdriver.load.strategy,unstable);

    尝试{
    binary.startProfile(torProfile,torProfileDir,);
    } catch(IOException e){
    e.printStackTrace();


  • 打开Firefox

      FirefoxProfile配置文件= 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 =新的FirefoxDriver(profile);


  • 关闭浏览器。请注意,如果你打算做大量的关闭和重新打开(有用的获得一个新的IP地址),我建议设置配置文件首选项 toolkit.startup.max_resumed_crashes 例如 9999

      private void killFirefox(){
    运行时rt = Runtime.getRuntime();

    尝试{
    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;
    字符串行;
    尝试{
    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;



Because Tor Browser Bundle is just a patched version of Firefox, it seems that it should be possible to use a FirefoxDriver with Tor Browser. This is what I've tried so far:

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");

This results in a blank Tor Browser page opening with a popup message: Your Firefox profile cannot be loaded. It may be missing or inaccessible.

I know that the profile is valid/compatible because I can successfully start the browser and profile with:

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

I don't know how to send commands to a browser opened in such a manner, however.

I've found similar questions, but I'm specifically looking for a Java solution, preferably tested on Windows.

I'm using a standalone Selenium library that can be downloaded here and the Tor Browser Bundle that can be downloaded here.

解决方案

Because Tor Browser Bundle wasn't letting me use the WebDriver extension, I found a workaround in which I ran Tor from a regular Firefox browser. With this method, as long as the Tor Browser is open, you can use Tor with a regular Firefox browser.

  • Open Tor Browser:

    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();
    }
    

  • Open Firefox with some configurations:

    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);
    

  • Close browsers. Note that if you plan on doing a lot of closing and reopening (useful in obtaining a new IP address), I advise setting the profile preference toolkit.startup.max_resumed_crashes to a high value like 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;
    }
    

这篇关于使用Selenium WebDriver和Tor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆