简体   繁体   中英

Tor Selenium works only in incognito mode Windows Python

This code runs the connection via Tor. You need to have Tor browser running beforehand to run this code successfully. However, that happens only when running an incognito window and the user profile is not loaded. When I remove that option it gets connected via my ISP. Anyway around this? To connect via Tor and have my user profile loaded. Also, if there is a window already open, it will not open the browser, because the User Data is already in use.

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


tor_proxy = "127.0.0.1:9150"

chrome_options = Options()


chrome_options.add_argument('--ignore-certificate-errors') 
chrome_options.add_argument('disable-infobars')
chrome_options.add_argument("--incognito")
chrome_options.add_argument("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data")
chrome_options.add_argument('--proxy-server=socks5://%s' % tor_proxy)
driver = webdriver.Chrome(executable_path='C:\\chromedriver.exe', options=chrome_options)
driver.get('https://www.wtfismyip.com')

INFO I think that the Tor browser's purpose would be defeated if it allowed loading signed-in profiles on subsequent runs. It is all about anonymity, if someone could just confirm that.

TO open tor you can use:

https://github.com/webfp/tor-browser-selenium

Steps:

First goto tor browser and type about:profiles:

在此处输入图像描述

THen open Tor Browser\Browser\TorBrowser\Tor folder:

There you can see a tor.exe file double click that:

在此处输入图像描述

Then run below code

from tbselenium.tbdriver import TorBrowserDriver
import time
driver= TorBrowserDriver(executable_path=r"C:\Users\prave\Downloads\travelBA\geckodriver.exe", tbb_fx_binary_path=r'C:\Users\prave\Desktop\Tor Browser\Browser\firefox.exe', tbb_profile_path=r'C:\Users\prave\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default')
driver.get('check.torproject.org')
time.sleep(100000)

Note: if you get a error saying filenotfound blabla/browser create an empty folder named broser at that location

Ignore below commands:

Could you try the below code, the right way to set sock proxy

from selenium import webdriver
import time
from selenium.webdriver.common.proxy import *
from selenium.webdriver.chrome.options import Options

PROXY = "127.0.0.1:9150"
webdriver.DesiredCapabilities.CHROME['proxy'] = {
    "httpProxy": PROXY,
    "ftpProxy": PROXY,
    "sslProxy": PROXY,
    "proxyType": "MANUAL",
    "socksProxy": PROXY,
    "socksVersion":5

}

webdriver.DesiredCapabilities.CHROME['acceptSslCerts'] = True
chrome_options=Options()
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('disable-infobars')
chrome_options.add_argument("--incognito")
chrome_options.add_argument(
    r"user-data-dir=C:\Users\prave\AppData\Local\Google\Chrome\User Data")

driver = webdriver.Chrome(
    executable_path=r'C:\Users\prave\Downloads\travelBA\chromedriver.exe', options=chrome_options)
driver.get('https://www.wtfismyip.com')

time.sleep(10000)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM