Package: tbselenium
tor-browser-seleniumA Python library to automate Tor Browser with Selenium WebDriver.📦 Installationpip install tbseleniumDownloadgeckodriverv0.31.0 from thegeckodriver releases pageand add it to PATH.🚀 UsageDownload and extractTor Browser, and pass its path when you initializeTorBrowserDriver. In the examples below, you should not pass "/path/to/tor-browser/" but the (Tor Browser) folder that contains the directory calledBrowser:Using with systemtortorneeds to be installed (apt install tor) and running on port 9050.fromtbselenium.tbdriverimportTorBrowserDriverwithTorBrowserDriver("/path/to/tor-browser/")asdriver:driver.get('https://check.torproject.org')Using withStemYou can useStemto start a new tor process programmatically, and connect to it fromtor-browser-selenium. Make sure you haveSteminstalled:pip install stem:importtbselenium.commonascmfromtbselenium.tbdriverimportTorBrowserDriverfromtbselenium.utilsimportlaunch_tbb_tor_with_stemtbb_dir="/path/to/tor-browser/"tor_process=launch_tbb_tor_with_stem(tbb_path=tbb_dir)withTorBrowserDriver(tbb_dir,tor_cfg=cm.USE_STEM)asdriver:driver.load_url("https://check.torproject.org")tor_process.kill()💡 ExamplesCheck theexamplesto discover different ways to usetor-browser-seleniumcheck_tpo.py: Visit thecheck.torproject.orgwebsite and print the network status messageheadless.py: Headless visit and screenshot of check.torproject.org usingPyVirtualDisplayonion_service.py: Search using DuckDuckGo's Onion serviceparallel.py: Visit `check.torproject.org`` with 3 browsers running in parallelscreenshot.py: Take a screenshotstem_simple.py: Use Stem to start atorprocessstem_adv.py: Use Stem to launchtorwith more advanced configuration🛠️ Test and developmentBrowse theexisting teststo find out about different ways you can usetor-browser-selenium.For development and testing first install the necessary Python packages:pip install -r requirements-dev.txtInstall thexvfbpackage by runningapt-get install xvfbor 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 usingXvfb, so the browser window will not be visible.
You may disableXvfbby setting theNO_XVFBenvironment variable:export NO_XVFB=1Running individual testsFirst, export the path to Tor Browser folder in theTBB_PATHenvironment variable.export TBB_PATH=/path/to/tbb/tor-browser/Then, usepy.testto launch the tests you want, e.g.:py.test tbselenium/test/test_tbdriver.pypy.test tbselenium/test/test_tbdriver.py::TBDriverTest::test_should_load_check_tpoUsing a customgeckodriverA customgeckodriverbinary can be set via theexecutable_pathargument:TorBrowserDriver(executable_path="/path/to/geckodriver")Disabling console logsYou can redirect the logs to/dev/nullby passing thetbb_logfile_pathinitialization parameter:TorBrowserDriver(...,tbb_logfile_path='/dev/null')⚙️ CompatibilityWarning:Windows and macOS are not supported.Testedwith the following Tor Browser versions on Ubuntu:Stable: 13.0.8Alpha: 13.5a3If you need to use a different version of Tor Browser,view the past test runsto find out the compatibleseleniumandgeckodriverversions.🔧 TroubleshootingSolutions to potential issues:Make sure you have compatible dependencies. While older or newer versions may work, they may cause issues.Tor Browserneeds to be downloaded and extracted.Pythonselenium(pip install -U selenium).geckodriverversion 0.31.0.Running Firefox on the same system may help diagnose issues such as missing libraries and displays.Process unexpectedly closed with status 1: If you encounter this on a remote machine you connect via SSH, you may need to enable theheadless mode.Port conflict with other (Tor) process: Pick a different SOCKS and controller port using thesocks_portargument.Usetbb_logfile_pathargument of TorBrowserDriver to debug obscure errors. This can help with problems due to missing display, missing libraries (e.g. when the LD_LIBRARY_PATH is not set correctly) or other errors that Tor Browser logs to standard output/error.driver.get_cookies()returns an empty list. This is due to Private Browsing Mode (PBM), which Selenium uses under the hood. See#79for a possible solution.WebGL is not supported in the headless mode started withheadless=Truedue to a Firefox bug (#1375585). To enable WebGL in a headless setting, usepyvirtualdisplayfollowing theheadless.pyexample.📚 ReferencePlease use the following reference if you usetor-browser-seleniumin your academic publications.@misc{tor-browser-selenium,
  author = {Gunes Acar and Marc Juarez and individual contributors},
  title = {tor-browser-selenium - Tor Browser automation with Selenium},
  year = {2023},
  publisher = {GitHub},
  howpublished = {\url{https://github.com/webfp/tor-browser-selenium}}
}🙌 CreditsWe greatly benefited from thetor-browser-bundle-testsuiteandtor-browser-seleniumprojects.