Tor Browser automation with Selenium.
A Python library to automate Tor Browser with Selenium.
pip install tbselenium
geckodriverfrom the geckodriver releases page. Make sure you install version v0.23.0 version or newer; older versions may not be compatible with the current Tor Browser series.
torneeds to be installed (
apt install tor) and running on port 9050.
from tbselenium.tbdriver import TorBrowserDriver with TorBrowserDriver("/path/to/TorBrowserBundle/") as driver: driver.get('https://check.torproject.org')
First, make sure you have
pip install stem). The following will start a new
Stem. It will not use the
torinstalled on your system.
import tbselenium.common as cm from tbselenium.tbdriver import TorBrowserDriver from tbselenium.utils import launch_tbb_tor_with_stem
tbb_dir = "/path/to/TorBrowserBundle/" 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")
TorBrowserDriver does not download Tor Browser Bundle (TBB) for you. You should download, extract TBB and provide its path when you initialize
Install the Python packages that are needed for development and testing:
pip install -r requirements-dev.txt
xvfbpackage by running
apt-get install xvfbor using your distro's package manager.
Run the following to launch the tests:
By default, tests will be run using
Xvfb, so the browser will not be visible. You may disable
Xvfbby exporting the following environment variable:
First, export a
TBB_PATHenvironment variable that points to the TBB version you want to use:
py.testto launch the tests you want, e.g.:
Check the examples to discover different ways to use TorBrowserDriver * check_tpo.py: Visit check.torproject.org website and print the network status message * headless.py: Headless visit and screenshot of check.torproject.org using XVFB * hidden_service.py: Search using DuckDuckGo's hidden service * parallel.py: Visit check.torproject.org with 3 browsers running in parallel * screenshot.py: Take a screenshot * stem_simple.py: Using Stem to start the Tor process * stem_adv.py: Using Stem with more advanced configuration
Tested with the following Tor Browser Bundle versions on Debian and Ubuntu:
Windows and macOS are not supported.
Solutions to potential issues:
seleniumpackage: This is the source of various obscure errors. Make sure you have
seleniumversion 3.3 or above.
Tor) process: Pick a different SOCKS and controller port using
tbb_logfile_pathargument of TorBrowserDriver to debug obscure errors. This can help with problems due to missing display, missing libraries (e.g. when the LDLIBRARYPATH is not set correctly) or other errors that Tor Browser logs to standard output/error.
LAUNCH_NEW_TBB_TORoption and get the following error message during the initialization, it's likely that Tor failed to bootstrap (due to network etc.):
Can't load the profile. Profile Dir: /tmp/tmpO7i1lL/webdriver-py-profilecopy If you specified a log_file in the FirefoxBinary constructor, check it for details
driver.get_cookies()returns an empty list. This is due to Private Browsing Mode (PBM), which Selenium uses under the hood. See #79 for a possible solution.
headless=Truedue to Firefox bug #1375585. To enable WebGL in a headless setting, use
pyvirtualdisplayfollowing the headless.py example.
set_security_leveldoesn't work with the current alpha (10.5a1).