Open tor browser with selenium

后端 未结 10 1100
长发绾君心
长发绾君心 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 10:09
    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
    from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
    
    #path to TOR binary
    binary = FirefoxBinary(r'...\Tor Browser\Browser\firefox.exe')
    #path to TOR profile
    profile = FirefoxProfile(r'...\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default')
    
    driver = webdriver.Firefox(firefox_profile= profile, firefox_binary= binary)
    driver.get("http://icanhazip.com")
    driver.save_screenshot("screenshot.png")
    driver.quit()
    

    Using Python 3.5.1 on Windows 10

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

    To open tor browser with Selenium driven GeckoDriver you need to:

    • Download and install the TOR Browser

    • Download the latest GeckoDriver v0.26.0 and place it in your system.

    • Install the recent Mozilla Firefox v77.0.1 browser.

    • You can use the following code block to open the TOR enabled browser:

      from selenium import webdriver
      from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
      import os
      
      torexe = os.popen(r'C:\Users\username\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
      profile = FirefoxProfile(r'C:\Users\username\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default')
      profile.set_preference('network.proxy.type', 1)
      profile.set_preference('network.proxy.socks', '127.0.0.1')
      profile.set_preference('network.proxy.socks_port', 9050)
      profile.set_preference("network.proxy.socks_remote_dns", False)
      profile.update_preferences()
      firefox_options = webdriver.FirefoxOptions()
      firefox_options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
      driver = webdriver.Firefox(firefox_profile= profile, options = firefox_options, executable_path=r'C:\WebDrivers\geckodriver.exe')
      driver.get("http://check.torproject.org")
      
    • Browser Snapshot:

    torFirefox_torproject


    Alternative using Firefox Nightly

    As an alternative you can also download, install and use the recent Firefox Nightly v79.0a1 browser.

    • Code Block:

      from selenium import webdriver
      from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
      import os
      
      torexe = os.popen(r'C:\Users\username\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
      profile = FirefoxProfile(r'C:\Users\username\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default')
      profile.set_preference('network.proxy.type', 1)
      profile.set_preference('network.proxy.socks', '127.0.0.1')
      profile.set_preference('network.proxy.socks_port', 9050)
      profile.set_preference("network.proxy.socks_remote_dns", False)
      profile.update_preferences()
      firefox_options = webdriver.FirefoxOptions()
      firefox_options.binary_location = r'C:\Program Files\Firefox Nightly\firefox.exe'
      driver = webdriver.Firefox(firefox_profile= profile, options = firefox_options, executable_path=r'C:\WebDrivers\geckodriver.exe')
      driver.get("http://check.torproject.org")
      
    • Browser Snapshot:

    torNightly_torproject


    Alternative using Chrome

    As an alternative you can also download, install and use the recent Chrome v84 browser.

    • Code Block:

      from selenium import webdriver
      import os
      
      torexe = os.popen(r'C:\Users\username\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
      PROXY = "socks5://localhost:9050" # IP:PORT or HOST:PORT
      options = webdriver.ChromeOptions()
      options.add_argument('--proxy-server=%s' % PROXY)
      driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
      driver.get("http://check.torproject.org")
      
    • Browser Snapshot:

    torChrome_torproject


    References

    You can find a couple of relevant detailed discussions in:

    • How to connect to Tor browser using Python
    • How to use Tor with Chrome browser through Selenium
    0 讨论(0)
  • 2020-12-02 10:13

    Don't use the TBB, just set the correct proxy settings in whatever browser you're using. In FF for example, like this:

    #set some privacy settings
    ff_prof.set_preference( "places.history.enabled", False )
    ff_prof.set_preference( "privacy.clearOnShutdown.offlineApps", True )
    ff_prof.set_preference( "privacy.clearOnShutdown.passwords", True )
    ff_prof.set_preference( "privacy.clearOnShutdown.siteSettings", True )
    ff_prof.set_preference( "privacy.sanitize.sanitizeOnShutdown", True )
    ff_prof.set_preference( "signon.rememberSignons", False )
    ff_prof.set_preference( "network.cookie.lifetimePolicy", 2 )
    ff_prof.set_preference( "network.dns.disablePrefetch", True )
    ff_prof.set_preference( "network.http.sendRefererHeader", 0 )
    
    #set socks proxy
    ff_prof.set_preference( "network.proxy.type", 1 )
    ff_prof.set_preference( "network.proxy.socks_version", 5 )
    ff_prof.set_preference( "network.proxy.socks", '127.0.0.1' )
    ff_prof.set_preference( "network.proxy.socks_port", 9050 )
    ff_prof.set_preference( "network.proxy.socks_remote_dns", True )
    
    #if you're really hardcore about your security
    #js can be used to reveal your true i.p.
    ff_prof.set_preference( "javascript.enabled", False )
    
    #get a huge speed increase by not downloading images
    ff_prof.set_preference( "permissions.default.image", 2 )
    
    ##
    # programmatically start tor (in windows environment)
    ##
    tor_path = "C:\\this\\is\\the\\location\\of\\" #tor.exe
    torrc_path = "C:\\you\\need\\to\\create\\this\\file\\torrc"
    
    DETACHED_PROCESS = 0x00000008
    #calling as a detached_process means the program will not die with your python program - you will need to manually kill it
    ##
    # somebody please let me know if there's a way to make this a child process that automatically dies (in windows)
    ##
    tor_process = subprocess.Popen( '"' + tor_path+'tor.exe" --nt-service "-f" "' + torrc_path + '"', creationflags=DETACHED_PROCESS )
    
    #attach to tor controller
    ## imports ##
    # import stem.socket
    # import stem.connection
    # import stem.Signal
    ##
    tor_controller = stem.socket.ControlPort( port=9051 )
    
    control_password = 'password'
    #in your torrc, you need to store the hashed version of 'password' which you can get with: subprocess.call( '"' + tor_path+'tor.exe" --hash-password %s' %control_password )
    
    stem.connection.authenticate( tor_controller, password=control_password )
    
    #check that everything is good with your tor_process by checking bootstrap status
    tor_controller.send( 'GETINFO status/bootstrap-phase' )
    response = worker.tor_controller.recv()
    response = response.content()
    
    #I will leave handling of response status to you
    
    0 讨论(0)
  • 2020-12-02 10:15

    A lot of answers are towards the right direction but this is exactly what worked for me:

    On Ubuntu:

    You need to install Tor using apt command or other method, but not the binary version.

    Installation guide:

    https://linuxconfig.org/how-to-install-tor-browser-in-ubuntu-18-04-bionic-beaver-linux

    Inside the sample.py you might need to:

    • set profile of the Firefox to torrc which is located most of the times in /etc/tor/.
    • set the binary to the Firefox binary of Tor, since Tor is just a series of configurations built atop of Firefox.

    You also need the geckodriver to automate firefox with selenium:

    • https://github.com/mozilla/geckodriver/releases (works with 0.21.0)
    • Extract
    • chmod +x geckodriver
    • export PATH=$PATH:/path-to-extracted-file/geckodriver

    Pay attension to the:

    • "network.proxy.socks_port" = 9150
    • Inside torrc ControlPort 9050, CookieAuthentication 1
    • Open TorBrowser
    • sudo lsof -i -P -n | grep LISTEN the LISTEN port of the tor network must be the same in the script
    • Run the python script, while TorBrowser is open

    Thanks user2426679 https://stackoverflow.com/a/21836296/3816638 for the settings.

    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
    from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
    from selenium.webdriver.common.proxy import Proxy, ProxyType
    from selenium.webdriver.firefox.options import Options
    import subprocess
    import os
    
    profileTor = '/etc/tor/' #  torrc
    binary = os.path.expanduser("~/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US/Browser/firefox")
    
    firefox_binary = FirefoxBinary(binary)
    firefox_profile = FirefoxProfile(profileTor)
    
    
    #set some privacy settings
    firefox_profile.set_preference( "places.history.enabled", False )
    firefox_profile.set_preference( "privacy.clearOnShutdown.offlineApps", True )
    firefox_profile.set_preference( "privacy.clearOnShutdown.passwords", True )
    firefox_profile.set_preference( "privacy.clearOnShutdown.siteSettings", True )   
    firefox_profile.set_preference( "privacy.sanitize.sanitizeOnShutdown", True )
    firefox_profile.set_preference( "signon.rememberSignons", False )
    firefox_profile.set_preference( "network.cookie.lifetimePolicy", 2 )
    firefox_profile.set_preference( "network.dns.disablePrefetch", True )
    firefox_profile.set_preference( "network.http.sendRefererHeader", 0 )
    
    #set socks proxy
    firefox_profile.set_preference( "network.proxy.type", 1 )
    firefox_profile.set_preference( "network.proxy.socks_version", 5 )
    firefox_profile.set_preference( "network.proxy.socks", '127.0.0.1' )
    firefox_profile.set_preference( "network.proxy.socks_port", 9150 )
    firefox_profile.set_preference( "network.proxy.socks_remote_dns", True )
    
    #if you're really hardcore about your security
    #js can be used to reveal your true i.p.
    firefox_profile.set_preference( "javascript.enabled", False )
    
    #get a huge speed increase by not downloading images
    firefox_profile.set_preference( "permissions.default.image", 2 )
    
    options = Options()
    options.set_headless(headless=False)
    
    driver = webdriver.Firefox(firefox_profile=firefox_profile,firefox_options=options)
    
    print(driver)
    
    driver.get("https://check.torproject.org/")
    driver.save_screenshot("screenshot.png")
    
    0 讨论(0)
提交回复
热议问题