I’m trying to automate interactions with the Tor Browser using Selenium WebDriver in Python. My script launches the Tor Browser successfully with a SOCKS5 proxy, but I’m encountering an issue when trying to click the connect button after the browser loads.
Here’s my Python script:
import time
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
tor_path = r'C:\Users\anjoyer\Desktop\Tor Browser\Browser\firefox.exe'
firefox_options = Options()
firefox_options.binary_location = tor_path
firefox_options.add_argument('--proxy-server=socks5://127.0.0.1:9150')
driver = webdriver.Firefox(options=firefox_options)
def wait_n_reload():
wait()
driver.refresh()
wait()
def wait(min_delay=5.0, max_delay=15.0, variability=0.1):
base_delay = np.random.normal(loc=(min_delay + max_delay) / 2, scale=(max_delay - min_delay) / 4)
base_delay = max(min_delay, min(base_delay, max_delay))
micro_delays = np.random.normal(loc=0, scale=variability, size=5)
total_delay = base_delay + sum(micro_delays)
if random.random() < 0.05:
total_delay += random.uniform(1, 3)
total_delay = max(0, total_delay)
time.sleep(total_delay)
return total_delay
time.sleep(60)
element = WebDriverWait(driver, 60).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="connectButton"]'))
)
element.click()
time.sleep(300)
When running the above script, I consistently encounter a TimeoutException when attempting to click the connect button in the Tor Browser. The browser window opens correctly and I’ve verified that the element is visible and interactable manually. However, Selenium seems to have trouble clicking the button.
Error Message:
Traceback (most recent call last):
File "c:UsersanjoyerDesktoptorautomation.py", line 46, in <module>
element = WebDriverWait(driver, 60).until(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersanjoyerAppDataLocalProgramsPythonPython312Libsite-packagesseleniumwebdriversupportwait.py", line 105, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
RemoteError@chrome://remote/content/shared/RemoteError.sys.mjs:8:8
WebDriverError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:187:5
NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:505:5
element.find/</<@chrome://remote/content/marionette/element.sys.mjs:135:16
What I tried:
Verified the XPath of the “connect” button.
Increased WebDriverWait timeout to 60 seconds.
Confirmed element visibility and interactability manually.
Expected Outcome:
Selenium waits for the “connect” button to be clickable within 60 seconds and successfully clicks it, initiating the connection in the Tor Browser.
Actual Result:
TimeoutException is raised when attempting to click the “connect” button, despite its visibility and interactability verified manually.
anjoyer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.