Tor Browser automation with Selenium
Project description
tor-browser-selenium 
A Python library to automate Tor Browser with Selenium.
Installation
pip install tbselenium
Install geckodriver from 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.
Basic usage
Using with system tor
tor needs 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')
Using with Stem
First, make sure you have Stem installed (pip install stem).
The following will start a new tor process using Stem. It will not use the tor installed 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")
tor_process.kill()
TorBrowserDriver does not download Tor Browser Bundle (TBB) for you. You should download, extract TBB and provide its path when you initialize TorBrowserDriver.
Test and development
Install the Python packages that are needed for development and testing:
pip install -r requirements-dev.txt
Install 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/TorBrowserBundle/
By default, tests will be run using Xvfb, so the browser will not be visible.
You may disable Xvfb by exporting the following environment variable:
export NO_XVFB=1
Running individual tests
First, export a TBB_PATH environment variable that points to the TBB version you want to use:
export TBB_PATH=/path/to/tbb/tor-browser_en-US/
Then, use py.test to 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_tpo
Examples
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
Compatibility
Tested with the following Tor Browser Bundle versions on Debian and Ubuntu:
- 10.0.1
- 10.5a1
Windows and macOS are not supported.
Troubleshooting
Solutions to potential issues:
- Make sure you can run Firefox on the same system. This may help discover various issues such as missing libraries, displays etc..
- Outdated (or incompatible) Python
seleniumpackage: This is the source of various obscure errors. Make sure you haveseleniumversion 3.3 or above. - No display: When running on a cloud machine, follow the headless.py example to start a virtual display.
- Outdated Tor Browser Bundle: Download and use a more recent TBB version.
- Make sure you install the latest geckodriver version.
- Port conflict with other (
Tor) process: Pick a different SOCKS and controller port usingsocks_portargument. - Use
tbb_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. - When you use
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.- WebGL is not supported in the headless mode started with
headless=Truedue to Firefox bug #1375585. To enable WebGL in a headless setting, usepyvirtualdisplayfollowing the headless.py example. set_security_leveldoesn't work with the current alpha (10.5a1).
Credits
We greatly benefited from the following two projects:
- tor-browser-selenium by @isislovecruft.
- tor-browser-bundle-testsuite by @boklm.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tbselenium-0.5.2-py2.py3-none-any.whl.
File metadata
- Download URL: tbselenium-0.5.2-py2.py3-none-any.whl
- Upload date:
- Size: 24.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.2 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.6.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
84dcc3f250b0c6bb4ce4bdb0d62de1d086343334019accb5469c21897afebb06
|
|
| MD5 |
f3b205301f30d50658bb09a406c46a65
|
|
| BLAKE2b-256 |
1b9040305d6b62ded9b333ec627002578b5ff937f3db3c614403862329b5958a
|