Open tor browser with selenium

后端 未结 10 1142
长发绾君心
长发绾君心 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: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

提交回复
热议问题