error while opening tor browser using Python selenium firefoxdriver

538 Views Asked by At

i am trying to open tor browser using selenium python

this is the code

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import time
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

#path to TOR binary
binary = FirefoxBinary(r'C:\Users\OKILAN\Desktop\Tor 
Browser\Browser\firefox.exe')
#path to TOR profile
profile = FirefoxProfile(r'C:\Users\OKILAN\Desktop\Tor 
Browser\Browser\TorBrowser\Data\Browser\profile.default')
cap = DesiredCapabilities().FIREFOX
cap["marionette"] = False
driver =webdriver.Firefox(capabilities=cap,firefox_binary=binary,executable_path=r'C:\Users\OKILAN\Desktop\geckodriver.exe')
driver.get("http://stackoverflow.com")
time.sleep(5)

driver.quit()

When i run the code it shows tor failed to start and i got the below error

Traceback (most recent call last):
File "C:\Users\OKILAN\Desktop\test1.py", line 13, in <module>
driver = webdriver.Firefox(capabilities=cap,firefox_binary=binary,executable_path=r'C:\Users\OKILAN\Desktop\geckodriver.exe')
File "C:\Users\OKILAN\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 191, in __init__
self.binary, timeout)
File "C:\Users\OKILAN\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\firefox\extension_connection.py", line 52, in __init__
self.binary.launch_browser(self.profile, timeout=timeout)
File "C:\Users\OKILAN\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\firefox\firefox_binary.py", line 73, in launch_browser
self._wait_until_connectable(timeout=timeout)
File "C:\Users\OKILAN\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\firefox\firefox_binary.py", line 104, in _wait_until_connectable
"The browser appears to have exited "
selenium.common.exceptions.WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.

help me to solve this error:-(

1

There are 1 best solutions below

3
On

You do not need all the binary and profile stuff, to create driver only use this line

driver = webdriver.Firefox(executable_path="C:\\geckodriver.exe")
driver.get("http://stackoverflow.com")

make sure you have downloaded and set geckodriver to environment 1

  1. First download GeckoDriver for Windows, extract it and copy the path to the folder.

  2. Right-click on My Computer or This PC. Select Properties.

  3. Select advanced system settings. Click on the Environment Variables button.
  4. From System Variables select PATH. Click on Edit button. Click New button. Paste the path of GeckoDriver file.