如何将Tor浏览器用作Web驱动程序硒?

时间:2018-09-18 21:45:04

标签: java selenium-webdriver

我遇到此异常:

  

线程“主”中的异常java.lang.IllegalStateException:驱动程序可执行文件的路径必须由webdriver.gecko.driver系统属性设置;否则,必须为false。有关更多信息,请参见https://github.com/mozilla/geckodriver。可以从https://github.com/mozilla/geckodriver/releases

下载最新版本

这是我的代码:

String torPath = "C:\\Users\\camil\\Desktop\\Tor Browser\\Browser\\firefox.exe";
String profilePath = "C:/Users/camil/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);

torProfile.setPreference("webdriver.load.strategy", "unstable");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setBinary(binary);
firefoxOptions.setProfile(torProfile);
FirefoxDriver driver = new FirefoxDriver(firefoxOptions);

1 个答案:

答案 0 :(得分:0)

使用现有浏览器配置文件成功启动Tor:

package navi;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class Tor_browser_test {
    private static WebDriver driver;

    @BeforeClass
    public static void setUpClass() {
        FirefoxOptions options = new FirefoxOptions();
        ProfilesIni allProfiles = new ProfilesIni();         
        FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");
        options.setProfile(selenium_profile);
        options.setBinary("C:\\Users\\pburgr\\Desktop\\Tor browser\\Browser\\firefox.exe");
        System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.20.0-win64\\geckodriver.exe");
        driver = new FirefoxDriver(options);
        driver.manage().window().maximize();}
    @Before
    public void setUp() {}
    @After
    public void tearDown() {}
    @AfterClass
    public static void tearDownClass() {driver.quit();}
    @Test
    public void tor_browser_test() throws InterruptedException {
        driver.get("https://www.google.com");
        Thread.sleep(5000);
    }
}