栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将Selenium WebDriver与Tor一起使用

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将Selenium WebDriver与Tor一起使用

因为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;    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/437529.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号