简体   繁体   English

使用Selenium WebDriver和Tor

[英]Using Selenium WebDriver with Tor

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. 因为Tor Browser Bundle只是Firefox的补丁版本,所以似乎可以使用带有Tor浏览器的FirefoxDriver 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. 这会导致打开一个空白的Tor浏览器页面,并显示一条弹出消息: 无法加载您的Firefox配置文件。 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. 我发现了类似的问题,但我特意寻找Java解决方案,最好在Windows上测试。

I'm using a standalone Selenium library that can be downloaded here and the Tor Browser Bundle that can be downloaded here . 我使用的是独立硒库,可以下载在这里和Tor浏览器套件,可以下载在这里

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. 因为Tor Browser Bundle不允许我使用WebDriver扩展,所以我找到了一种解决方法,我从常规的Firefox浏览器中运行Tor。 With this method, as long as the Tor Browser is open, you can use Tor with a regular Firefox browser. 使用此方法,只要Tor浏览器打开,您就可以使用常规Firefox浏览器使用Tor。

  • Open Tor Browser : 打开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(); } 
  • Open Firefox with some configurations: 使用一些配置打开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); 
  • 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 . 请注意,如果您计划进行大量关闭和重新打开(在获取新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; } 

I would try specifying the path of one of the existing profiles and initialize your profile instance for Tor so your code would look something like: 我会尝试指定其中一个现有配置文件的路径并初始化Tor的配置文件实例,因此您的代码看起来像:

String torPath = "..\\Tor Browser\\Browser\\firefox.exe";
String profilePath = "..\\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");

I didn't try this since I don't have WebDriver setup at home, but this should allow you to specify a profile. 我没有尝试这个,因为我家里没有WebDriver设置,但这应该允许你指定一个配置文件。

I downloaded TorBrowser and I was able to call it without any problem, with the following code (it's Mac OS). 我下载了TorBrowser,我可以毫无问题地调用它,使用以下代码(它是Mac OS)。 So I think there shouldn't be any problem about compatibility between the firefox webdriver and the latest version of Tor Browser. 所以我认为firefox webdriver和最新版本的Tor Browser之间的兼容性应该没有任何问题。

String torPath = "/Volumes/DATA/Downloads/Tor.app/Contents/MacOS/TorBrowser.app/Contents/MacOS/firefox";
String profilePath = "/Users/mimitantono/Library/Application Support/Firefox/Profiles/1vps9kas.default-1384778906995";
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/webhp?complete=1&hl=en");

I know that you already tested the path of your profile with binary.startProfile but I think you could try again to use slash instead of backslash to specify the path, cross check whether that file exists -> profile.default , and to use absolute path instead of relative -> ../ . 我知道您已经使用binary.startProfile测试了配置文件的路径,但我认为您可以再次尝试使用斜杠而不是反斜杠来指定路径,交叉检查该文件是否存在 - > profile.default ,并使用绝对路径而不是相对 - > ../

This code also is working pretty good in ubuntu. 这段代码在ubuntu中也运行得很好。 Here is an example (JUnit4): 这是一个例子(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

    }
}

If you mostly care about remote-controlling a browser using Tor (and would maybe prefer to use the Tor Browser instead of vanilla Firefox), you can use Mozilla's Marionette . 如果您最关心使用Tor远程控制浏览器(并且可能更喜欢使用Tor浏览器而不是使用Torilla Firefox),则可以使用Mozilla的Marionette Use like 用得像

To use with the Tor Browser, enable marionette at startup via 要与Tor浏览器一起使用,请在启动时启用marionette

Browser/firefox -marionette

(inside the bundle). (在捆绑内)。 Then, you can connect via 然后,您可以通过连接

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

and load a new page for example via 并加载一个新页面,例如通过

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

For more examples, there is a tutorial along with more documentation. 有关更多示例,请参阅教程以及更多文档。

( copy ) 副本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM