DEAR PEOPLE FROM THE FUTURE: Here's what we've figured out so far...

Welcome! This is a Q&A website for computer programmers and users alike, focused on helping fellow programmers and users. Read more

What are you stuck on? Ask a question and hopefully somebody will be able to help you out!
+2 votes

Following the guide Scraping Websites with Python, Selenium, and Tor I don't know how could I wait only until the tor browser is connected instead of waiting a fixed amount of time. Something like what there is in the comment below, which isn't working right now. The torStatus is just an example with doesn't really exist. I'm looking for an actual way to tell when it is connected.

def connect_tor() -> None:
    driver.find_element(By.XPATH, '//*[@id="connectButton"]').click()
    #while driver.find_element(By.XPATH, '//*[@id="torStatus"]').get_attribute("value") != "connected":
        #time.sleep(1)
    time.sleep(20)
by

3 Answers

0 votes
 
Best answer

It was easier than I expected:

while driver.find_element(By.XPATH, '//*[@id="connectLongContent"]').is_displayed():
    time.sleep(1)

There is also the tor-browser-selenium library to consider.

by
edited by
0 votes

I don't think it's possible to check when the Tor Browser (Firefox) has established a connection with an Entry Relay. What you can do however is load a page and wait until a certain condition is met. If the page loaded, you know you are connected. This functionality is called "Explicit Wait" in Selenium.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver.get("https://check.torproject.org")
try:
    element = WebDriverWait(driver, 500).until(
        EC.presence_of_element_located((By.ID, "element-id"))
    )
finally:
    driver.quit()
by
0 votes

If you configure tor browser to connect automatically at startup, you do not need to .click() the connect button. Start the browser and wait until the homepage is loaded.

by
Contributions licensed under CC0
...