I’m trying to run two separate Python scripts on Windows 11, each using Tor and Selenium with Firefox. I’ve installed two separate Tor browsers in different directories and configured them to use different ports. However, the second script doesn’t start correctly and can’t connect.
First script:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
tor_path = r'C:\tor1\Tor Browser\Browser\firefox.exe'
firefox_options = Options()
# firefox_options.add_argument("--headless")
firefox_options.binary_location = tor_path
driver = webdriver.Firefox(options=firefox_options)
WebDriverWait(driver, 300).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="connectButton"]'))
)
driver.find_element(By.XPATH, value='//*[@id="connectButton"]').click()
time.sleep(60)
Second script:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
tor_path = r'C:\tor2\Tor Browser\Browser\firefox.exe'
firefox_options = Options()
# firefox_options.add_argument("--headless")
firefox_options.binary_location = tor_path
driver = webdriver.Firefox(options=firefox_options)
WebDriverWait(driver, 300).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="connectButton"]'))
)
driver.find_element(By.XPATH, value='//*[@id="connectButton"]').click()
time.sleep(60)
I’ve configured the torrc file in both Tor paths to use different ports:
First torrc configuration:
SocksPort 9072
ControlPort 9073
Second torrc configuration:
SocksPort 9074
ControlPort 9075
The first script runs without issues.
The second script fails to start correctly and cannot connect to Tor.
By “first” and “second,” I mean:
-
First script: The script using
tor_path = r'C:\tor1\Tor Browser\Browser\firefox.exe'
. This script runs perfectly without any issues. -
Second script: The script using
tor_path = r'C:\tor2\Tor Browser\Browser\firefox.exe'
. This is the script that fails to start correctly.
Here’s what I did:
-
I first ran the script with
tor_path = r'C:\tor1\Tor Browser\Browser\firefox.exe'
, and it worked fine. -
Then, I installed a second Tor browser in another directory and changed its
torrc
file to use different ports forSocksPort
andControlPort
. -
I updated the Python script to point to the
firefox.exe
in the second Tor folder (tor_path = r'C:\tor2\Tor Browser\Browser\firefox.exe'
).
When I start the second script, it doesn’t matter if the first one is running or not. Initially, when I wrote the question, the second script started but couldn’t connect, showing an error message as seen in the uploaded screenshot.
To troubleshoot, I added a second geckodriver
in the Tor folder and changed the data directory in the torrc
file. Now, when I run the second script, it just opens Firefox instead of Tor.
error
anjoyer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.