Python selenium tor proxy

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Tor Browser automation with Selenium.

License

webfp/tor-browser-selenium

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

A Python library to automate Tor Browser with Selenium WebDriver.

Download geckodriver v0.31.0 from the geckodriver releases page and add it to PATH.

Download and extract Tor Browser, and pass its path when you initialize TorBrowserDriver .

tor needs to be installed ( apt install tor ) and running on port 9050.

from tbselenium.tbdriver import TorBrowserDriver with TorBrowserDriver("/path/to/tor-browser/") as driver: driver.get('https://check.torproject.org')

You can use Stem to start a new tor process programmatically, and connect to it from tor-browser-selenium . Make sure you have Stem installed: pip install stem :

import tbselenium.common as cm from tbselenium.tbdriver import TorBrowserDriver from tbselenium.utils import launch_tbb_tor_with_stem tbb_dir = "/path/to/tor-browser/" tor_process = launch_tbb_tor_with_stem(tbb_path=tbb_dir) with TorBrowserDriver(tbb_dir, tor_cfg=cm.USE_STEM) as driver: driver.load_url("https://check.torproject.org") tor_process.kill()

Check the examples to discover different ways to use tor-browser-selenium

  • check_tpo.py: Visit the check.torproject.org website and print the network status message
  • headless.py: Headless visit and screenshot of check.torproject.org using PyVirtualDisplay
  • onion_service.py: Search using DuckDuckGo’s Onion service
  • parallel.py: Visit `check.torproject.org« with 3 browsers running in parallel
  • screenshot.py: Take a screenshot
  • stem_simple.py: Use Stem to start a tor process
  • stem_adv.py: Use Stem to launch tor with more advanced configuration
  • Browse the existing tests to find out about different ways you can use tor-browser-selenium .
  • For development and testing first install the necessary Python packages: pip install -r requirements-dev.txt
  • Install the xvfb package by running apt-get install xvfb or using your distro’s package manager.
  • Run the following to launch the tests: ./run_tests.py /path/to/tor-browser/
  • By default, tests will be run using Xvfb , so the browser window will not be visible. You may disable Xvfb by setting the NO_XVFB environment variable: export NO_XVFB=1
  • Then, use py.test to launch the tests you want, e.g.:
  • py.test tbselenium/test/test_tbdriver.py
  • py.test tbselenium/test/test_tbdriver.py::TBDriverTest::test_should_load_check_tpo

Using a custom geckodriver

A custom geckodriver binary can be set via the executable_path argument:

TorBrowserDriver(executable_path="/path/to/geckodriver")

You can redirect the logs to /dev/null by passing the tbb_logfile_path initialization parameter:

TorBrowserDriver(. tbb_logfile_path='/dev/null')

Warning: Windows and macOS are not supported.

Tested with the following Tor Browser versions on Ubuntu:

If you need to use a different version of Tor Browser, view the past test runs to find out the compatible selenium and geckodriver versions.

Solutions to potential issues:

  • Make sure you have compatible dependencies. While older or newer versions may work, they may cause issues.
    • Tor Browser needs to be downloaded and extracted.
    • Python selenium ( pip install -U selenium ).
    • geckodriver version 0.31.0.

    Please use the following reference if you use tor-browser-selenium in your academic publications.

    @misc, title = , year = , publisher = , howpublished = <\url> > 

    Источник

    Python Selenium binding with TOR browser

    Traceback (most recent call last): File «C:/Python34/torfirstscript.py», line 10, in browser=webdriver.Firefox( binary, profile) File «C:\Python34\lib\site-packages\selenium-2.43.0-py3.4.egg\selenium\webdriver\firefox\webdriver.py», line 46, in init self.NATIVE_EVENTS_ALLOWED and self.profile.native_events_enabled) AttributeError: ‘FirefoxBinary’ object has no attribute ‘native_events_enabled’

    browser=webdriver.Firefox( firefox_binary = binary, firefox_profile = profile) 

    Traceback (most recent call last): File «C:\Python34\torfirstscript.py», line 9, in browser=webdriver.Firefox( firefox_binary = binary, firefox_profile = >profile) File «C:\Python34\lib\site-packages\selenium-2.43.0->py3.4.egg\selenium\webdriver\firefox\webdriver.py», line 59, in init self.binary, timeout), File «C:\Python34\lib\site-packages\selenium-2.43.0->py3.4.egg\selenium\webdriver\firefox\extension_connection.py», line 47, in >init self.binary.launch_browser(self.profile) File «C:\Python34\lib\site-packages\selenium-2.43.0->py3.4.egg\selenium\webdriver\firefox\firefox_binary.py», line 64, in launch_browser self._wait_until_connectable() File «C:\Python34\lib\site-packages\selenium-2.43.0-py3.4.egg\selenium\webdriver\firefox\firefox_binary.py», line 108, in _wait_until_connectable self.profile.path, self._get_firefox_output())) selenium.common.exceptions.WebDriverException: Message: «Can’t load the profile. Profile Dir: >C:\Users\HPPAV1~1\AppData\Local\Temp\tmpig7zvx_0\webdriver-py-profilecopy Firefox output: None»

    enter image description here

    with that image as output.

    Источник

    How to connect to Tor browser using Python

    I am trying to connect to a Tor browser but get an error stating «proxyConnectFailure» any ideas I have tried multiple attempts to get into the basics of Tor browser to get it connected but all in vain if any could help life could be saved big time:

    from selenium import webdriver from selenium.webdriver.firefox.firefox_profile import FirefoxProfile from selenium.webdriver.firefox.firefox_binary import FirefoxBinary binary = FirefoxBinary(r"C:\Users\Admin\Desktop\Tor Browser\Browser\firefox.exe") profile = FirefoxProfile(r"C:\Users\Admin\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default") # Configured profile settings. proxyIP = "127.0.0.1" proxyPort = 9150 proxy_settings = driver = webdriver.Firefox(firefox_binary=binary,proxy=proxy_settings) def interactWithSite(driver): driver.get("https://www.google.com") driver.save_screenshot("screenshot.png") interactWithSite(driver) 

    3 Answers 3

    To connect to a Tor Browser through a FirefoxProfile you can use the following solution:

    from selenium import webdriver from selenium.webdriver.firefox.firefox_profile import FirefoxProfile import os torexe = os.popen(r'C:\Users\AtechM_03\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe') profile = FirefoxProfile(r'C:\Users\AtechM_03\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() driver = webdriver.Firefox(firefox_profile= profile, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe') driver.get("http://check.torproject.org") 

    Tor_Python

    Can you crosscheck first if you are able to connect to the url http://check.torproject.org and see the message This browser is configured to use Tor. first?

    So basically you aren’t connected via Tor. Did you try to use the code block from the answer and gave it a try as it is?

    yes i did so basically it opens up the tor window when i manually enter check.torproject.org it opens up but not with code

    I would like to expand on @DebanjanB answer by adding the Linux counterpart:

    from selenium import webdriver from selenium.webdriver.firefox.firefox_profile import FirefoxProfile import os torexe = os.popen('some/path/tor-browser_en-US/Browser/start-tor-browser') # in my case, I installed it under a folder tor-browser_en-US after # downloading and extracting it from # https://www.torproject.org/download/ for linux profile = FirefoxProfile( 'some/path/tor-browser_en-US/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 = '/usr/bin/firefox' # /usr/bin/firefox is default location of firefox - for me anyway driver = webdriver.Firefox( firefox_profile=profile, options=firefox_options, executable_path='wherever/you/installed/geckodriver') # I keep my geckodriver(s) in a special folder sorted by versions. # Geckodriver downloadable here: # https://github.com/mozilla/geckodriver/releases/ driver.get("http://check.torproject.org") 

    The verified answer does not work in case of opening dot onion sites(I believe that’s something to do with tor network which is not allowing access to normal firefox).

    As for the latest tor browser (from the tor browser bundle), starting it using selenium causes some error due to which the browser cannot start tor proxy itself causing proxy and timeout errors(doesn’t matter if tor proxy is started by python or manually or not started at all). This could also be due to port 9050 or 9150 being used by tor proxy and not being available to browser’s tor instance but this does not explain the error caused when no instance of tor proxy is running.

    The solution i have found is to start the tor proxy as normal, manually or using os.popen(«tor.exe») and configure tor browser to not start tor proxy. here’s the code:

    from selenium import webdriver from selenium.webdriver.firefox.firefox_profile import FirefoxProfile from selenium.webdriver.firefox.firefox_binary import FirefoxBinary os.popen(r'e:\\bla\\bla\\bla\\tor\\Tor\\tor.exe') binary=FirefoxBinary(r'e:\\bla\\bla\\bla\\Tor Browser\\Browser\\firefox.exe') fp=FirefoxProfile(r'e:\\foo\\bar\\bla\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default') fp.set_preference('extensions.torlauncher.start_tor',False)#note this fp.set_preference('network.proxy.type',1) fp.set_preference('network.proxy.socks', '127.0.0.1') fp.set_preference('network.proxy.socks_port', 9050) fp.set_preference("network.proxy.socks_remote_dns", True) fp.update_preferences() driver = webdriver.Firefox(firefox_profile=fp,firefox_binary=binary) driver.get("http://check.torproject.org") driver.get('https://www.bbcnewsv2vjtpsuy.onion/') 

    *note fp.set_preference(‘extensions.torlauncher.start_tor’,False) on line 10 is being used to configure tor to not start its own tor instance so that it uses the proxy config and tor instance started above.

    lo and behold as the tbb starts working like normal firefox bot browser

    Источник

    Читайте также:  Экспорт excel mysql php
Оцените статью