使用Selenium WebDriver运行TOR浏览器



我目前正在尝试使用Selenium WebDriver (JAVA) 2.53和Firefox 43.0执行TOR 6.0.4。我已经按照这篇文章的说明使用Selenium WebDriver与Tor,但我得到一个错误,而加载Tor的profilePath到Firefox二进制文件。我已经看到有可能通过加载TOR profile.default存档到firefox二进制文件来午餐TOR,但我得到一个Driver info: Driver。版本:未知,当用配置文件实例化二进制文件时。我试过换火狐版本,但还是。在我启动驱动程序的代码下面。我也用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");

参见下面抛出的异常:

Exception in thread "main" org.openqa.selenium.WebDriverException: Specified firefox binary location does not exist or is not a real file: C:UsersJose BernhardtDesktopTor BrowserStart Tor Browser.exe

似乎我正在加载Tor.exe,而不是我必须从Tor存档加载firefox.exe。我改变了我的道路,这是有效的。还修复了我没有将配置文件和二进制文件发送到驱动程序构造函数

 "C:\Users\Jose Bernhardt\Desktop\Tor Browser\Browser\firefox.exe"
FirefoxDriver driver = new FirefoxDriver(binary, torProfile);
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);

目前Mozilla Firefox支持Tor浏览器下面是代码:

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

如果你想改变tor的标识,那么你必须重新启动tor使用,并再次启动上面的代码:

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

最新更新