将Selenium WebDriver与Tor一起使用

乔尔·克里斯多夫(Joel Christophel):

由于Tor浏览器捆绑包只是Firefox的修补版本,因此似乎应该可以FirefoxDriver在Tor浏览器中使用。到目前为止,这是我尝试过的:

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

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

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

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

但是,我不知道如何向打开的浏览器发送命令。

我发现了类似的问题,但是我正在寻找Java解决方案,最好在Windows上进行测试。

我使用的是一个独立的Selenium库,可以在这里下载而Tor浏览器捆绑包可以在这里下载

乔尔·克里斯多夫(Joel Christophel):

因为Tor浏览器捆绑包不允许我使用WebDriver扩展,所以我找到了一种解决方法,可以从常规的Firefox浏览器中运行Tor。使用此方法,只要打开Tor浏览器,就可以将Tor与常规Firefox浏览器一起使用。

  • 打开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();
    }
    
  • 使用某些配置打开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);
    
  • 关闭浏览器请注意,如果您打算进行很多关闭和重新打开操作(有助于获取新的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;
    }
    

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将AutoIT与Selenium一起使用

将Selenium WebDriver与Java一起使用跨度类getText()

将个人SSL证书与Webdriver一起使用(Selenium 2.0)

将 Chrome 驱动程序与 Selenium Webdriver 一起使用时出现 SocketException

通过将Selenium WebDriver与Java一起使用从Excel Sheet读取测试数据

IE WebDriver Selenium与Active Directory登录一起使用

将Highchart SVG图像与Selenium一起使用

如何使Rubysspi 1.3.1与Ruby 1.9.3和Selenium-Webdriver一起使用

如何在Mac OSX上使Firefox与Selenium WebDriver一起使用

Selenium webdriver 不能与 docker-compose 一起使用

如何设置MAC与Watir Webdriver和Ruby一起使用Selenium

Selenium 和 Cypress 一起使用?

无法将Selenium与Python一起播放YouTube视频

将CURL与TOR一起用作CentO上的代理

如何将Selenium与多个Url一起使用以写入csv

如何将Selenium与别名Python 3一起使用

我可以将Selenium方法与Intern / Leadfoot方法一起使用吗?

如何将Mobilenium Python模块与Selenium一起使用

将Selenium与Chrome 87和xvfb一起使用会导致屏幕变形

如何通过将Selenium与phantomjs一起使用从新窗口获取url

将Selenium与Edge Chromium和Javascript绑定一起使用时,出现“ WebDriverError:未知错误”

如何将Chrome驱动程序与Selenium一起使用Python登录到Applemusic

将 time.sleep 与 selenium 一起使用(睡眠直到完全加载某些内容)

将JMSListener与RabbitMQ一起使用

将ctypes与typedef一起使用

将Highchart与Codeigniter一起使用

将TOP与GROUP BY一起使用

将dnorm与RcppArmadillo一起使用

将“ -Filter”与变量一起使用