0

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:-(

Ismael Padilla
  • 5,246
  • 4
  • 23
  • 35

1 Answers1

0

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.
Adrian Jimenez
  • 979
  • 9
  • 23
  • HI Adrian , i have tried your answer and i got "selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities" error and i solved it by this discussion [link](https://stackoverflow.com/questions/47782650/selenium-common-exceptions-sessionnotcreatedexception-message-unable-to-find-a/47785513) and then i got a new error as follows "Failed to find firefox binary. You can set it by specifying " selenium.common.exceptions.WebDriverException: Message: Failed to find firefox binary. You can set it by specifying the path to 'firefox_binary': – Ruthran Smart Apr 21 '20 at 05:40
  • you did not apply what i suggested, i see you still using that binary stuff make sure you have geckodriver and that is it, you only need to call the line i mentioned above – Adrian Jimenez Apr 22 '20 at 16:43
  • i have updated the code as you said but i got a exception like Unable to find a matching set of capabilities as i said in the first comment after that only i have updated the code like this using some stack overflow discussion and suggestions – Ruthran Smart Apr 22 '20 at 22:04