Open tor browser with selenium

后端 未结 10 1099
长发绾君心
长发绾君心 2020-12-02 09:15

Is it possible to make selenium use the TOR browser? Does anyone have any code they could copy-paste?

相关标签:
10条回答
  • 2020-12-02 09:52

    Using ruby,

    profile = Selenium::WebDriver::Firefox::Profile.new
    profile.proxy = Selenium::WebDriver::Proxy.new :socks => '127.0.0.1:9050' #port where TOR runs
    browser = Watir::Browser.new :firefox, :profile => profile
    

    To confirm that you are using Tor, use https://check.torproject.org/

    0 讨论(0)
  • 2020-12-02 09:53

    As a newer alternative to Selenium, which only controls Firefox, have a look at Marionette. To use with the Tor Browser, enable marionette at startup via

    Browser/firefox -marionette
    

    (inside the bundle). Then, you can connect via

    from marionette import Marionette
    client = Marionette('localhost', port=2828);
    client.start_session()
    

    and load a new page for example via

    url='http://mozilla.org'
    client.navigate(url);
    

    For more examples, there is a tutorial.


    Older answer

    The Tor project has a selenium test for its browser. It works like:

    from selenium import webdriver
    ffbinary = webdriver.firefox.firefox_binary.FirefoxBinary(firefox_path=os.environ['TBB_BIN'])
    ffprofile = webdriver.firefox.firefox_profile.FirefoxProfile(profile_directory=os.environ['TBB_PROFILE'])
    self.driver = webdriver.Firefox(firefox_binary=ffbinary, firefox_profile=ffprofile)
    self.driver.implicitly_wait(30)
    self.base_url = "about:tor"
    self.verificationErrors = []
    self.accept_next_alert = True
    self.driver.get("http://check.torproject.org/")
    self.assertEqual("Congratulations. This browser is configured to use Tor.", driver.find_element_by_css_selector("h1.on").text)
    

    As you see, this uses the environment variables TBB_BIN and TBB_PROFILE for the browser bundle and profile. You might be able to hardcode these in your code.

    0 讨论(0)
  • 2020-12-02 09:55

    I looked into this, and unless I'm mistaken, on face value it's not possible.

    The reason this cannot be done is because:

    • Tor Browser is based on the Firefox code.
    • Tor Browser has specific patches to the Firefox code to prevent external applications communicating with the Tor Browser (including blocking the Components.Interfaces library).
    • The Selenium Firefox WebDriver communicates with the browser through Javascript libraries that are, as aforementioned, blocked by Tor Browser.

    This is presumably so no-one outside of the Tor Browser either on your box or over the internet knows about your browsing.

    Your alternatives are:

    • Use a Tor proxy through Firefox instead of the Tor Browser (see the link in the comments of the question).
    • Rebuild the Firefox source code with the Tor Browser patches excluding those that prevent external communication with Tor Browser.

    I suggest the former.

    0 讨论(0)
  • 2020-12-02 10:02

    //just check your tor browser's port number and change that accordingly in the //code

    from selenium import webdriver
    profile=webdriver.FirefoxProfile()
    profile.set_preference('network.proxy.type', 1)
    profile.set_preference('network.proxy.socks', '127.0.0.1')
    profile.set_preference('network.proxy.socks_port', 9150)
    browser=webdriver.Firefox(profile)
    browser.get("http://yahoo.com")
    browser.save_screenshot("screenshot.png")
    browser.close()
    
    0 讨论(0)
  • 2020-12-02 10:05
    System.setProperty("webdriver.firefox.marionette", "D:\\Lib\\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);
    
    0 讨论(0)
  • 2020-12-02 10:08

    Yes, it is possible to make selenium use the TOR browser.

    I was able to do so on both Ubuntu and Mac OS X.

    Two things have to happen:

    1. Set the binary path to the firefox binary that Tor uses. On a Mac this path would typically be /Applications/TorBrowser.app/Contents/MacOS/firefox. On my Ubuntu machine it is /usr/bin/tor-browser/Browser/firefox.

    2. The Tor browser uses a SOCKS host at 127.0.0.1:9150 either through Vidalia or Tor installation. Launch Tor once from the Finder and leave it open so that Vidalia will be running. The instances launched with selenium will use the SOCKS host that Vidalia starts, too.

    Here is the code to accomplish those two things. I run this on Mac OS X Yosemite:

    import os
    from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
    from selenium import webdriver
    
    
    # path to the firefox binary inside the Tor package
    binary = '/Applications/TorBrowser.app/Contents/MacOS/firefox'
    if os.path.exists(binary) is False:
        raise ValueError("The binary path to Tor firefox does not exist.")
    firefox_binary = FirefoxBinary(binary)
    
    
    browser = None
    def get_browser(binary=None):
        global browser  
        # only one instance of a browser opens, remove global for multiple instances
        if not browser: 
            browser = webdriver.Firefox(firefox_binary=binary)
        return browser
    
    if __name__ == "__main__":
        browser = get_browser(binary=firefox_binary)
        urls = (
            ('tor browser check', 'https://check.torproject.org/'),
            ('ip checker', 'http://icanhazip.com')
        )
        for url_name, url in urls:
            print "getting", url_name, "at", url
            browser.get(url)
    

    On an Ubuntu system I was able to run the Tor browser via selenium. This machine has tor running at port 9051 and privoxy http proxy that uses tor at port 8118. In order for the Tor browser to pass the tor check page I had to set the http proxy to privoxy.

    from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
    from selenium.webdriver.common.proxy import Proxy, ProxyType
    from selenium import webdriver
    browser = None
    
    proxy_address = "127.0.0.1:8118"
    proxy = Proxy({
        'proxyType': ProxyType.MANUAL,
        'httpProxy': proxy_address,
    })
    
    tor = '/usr/bin/tor-browser/Browser/firefox'
    firefox_binary = FirefoxBinary(tor)
    
    urls = (
        ('tor_browser_check', 'https://check.torproject.org/'),
        ('icanhazip', 'http://icanhazip.com'),
    )
    keys, _ = zip(*urls)
    urls_map = dict(urls)
    
    def get_browser(binary=None, proxy=None):
        global browser
        if not browser:
            browser = webdriver.Firefox(firefox_binary=binary, proxy=proxy)
        return browser
    
    if __name__ == "__main__":
        browser = get_browser(binary=firefox_binary, proxy=proxy)
        for resource in keys:
            browser.get(urls_map.get(resource))
    
    0 讨论(0)
提交回复
热议问题