I'm attempting to automate the use of the Tor browser through selenium:
import time
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = '/Applications/TorBrowser.app/Contents/MacOS/firefox'
firefox_binary = FirefoxBinary(binary)
try:
driver = webdriver.Firefox(firefox_binary="/Applications/Tor Browser.app/Contents/MacOS/firefox")
driver.get("https://www.google.com/")
time.sleep(3)
finally:
driver.close()
I am trying to use Tor browser for scraping and everything works fine, but every time I run code you have to manually click connect button. How do I disable the confirmation screen or automatic its acceptance from within selenium?
This tor browser configuration screen...
... comes up because you haven't configured TOR Browser proxy settings explicitly in your code block. So every time you execute your program, it initiates a new TOR browsing context without the proxy settings and assumes that you are running Tor Browser for the first time. Hence you will see the Tor Network Settings window on each run.
To get rid of the Tor Network Settings window, you need to configure the Tor Network Settings in your code block as follows:
Code Block:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
import os
torexe = os.popen(r'C:\Users\Soma Bhattacharjee\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
profile = FirefoxProfile(r'C:\Users\Soma Bhattacharjee\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default')
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9050)
profile.set_preference("network.proxy.socks_remote_dns", False)
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile= profile, executable_path=r'C:\WebDrivers\geckodriver.exe')
driver.get("http://check.torproject.org")
Browser Snapshot:
You can find a couple of detailed discussions in: