如何使用tor浏览器作为网络驱动程序selenium?

问题描述 投票:1回答:1

我得到这个例外:

线程“main”中的异常java.lang.IllegalStateException:驱动程序可执行文件的路径必须由webdriver.gecko.driver系统属性设置;有关更多信息,请参阅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);
java selenium-webdriver
1个回答
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);
    }
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.