Python selenium using TOR to get proxies works for firefox, but doesn't work for chrome
Asked Answered
M

2

9

For the last few days, I have been playing around with python, learned from youtube, and decided to create few things I would love to automate.

I have found the small problem - when I use my python script with Firefox browser, I can just open the Tor browser in the background and using this code:

profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.socks", '127.0.0.1')
profile.set_preference("network.proxy.socks_port", 9150)
profile.set_preference("network.proxy.socks_remote_dns", False)
profile.update_preferences()
browser = webdriver.Firefox(firefox_profile=profile)

Everything works, it opens web browser with new ip every time. Now I tried the same with Chrome, using this code

PROXY = "127.0.0.1:9150" # IP:PORT or HOST:PORT
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)
chrome = webdriver.Chrome(chrome_options=chrome_options)

I have tested this code with regular proxies (not from tor), and they did work indeed.

This is what happens to chrome when I do this: http://prntscr.com/kf8vzt

I was thinking, why did it work for Firefox, not for chrome, checked out that it might be because Tor is written based from firefox? (I might be wrong, if I am, please correct this).

Is it possible to use it with Chrome as well?

In chrome settings just tried to setup inside

Moreville answered 6/8, 2018 at 10:55 Comment(0)
K
6

To use Tor's SOCKS proxy server with chrome, include the socks protocol in the scheme with the --proxy-server option:

PROXY = "socks5://127.0.0.1:9150"

Chrome is defaulting to an HTTP proxy which is why it can't connect to sites when you launch.

Kevinkevina answered 6/8, 2018 at 15:1 Comment(3)
You are the best. Thank you very much! This fixed my problem. Can't add you feedback, but would do it if I could!Moreville
Glad that helped! At this point, you should be able to "accept" the answer by clicking the checkbox to the left of my answer score. Good luck with the coding!Kevinkevina
Can': Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score. But when I will br at home will edit my post with your answer so if someone else is looking I could help them out. Oh nvm found it. Was clicking on wrong thingMoreville
C
0

To anyone who still gets here. The port is changed to 9050 now on my Windows tor daemon. Perhaps it will keep changing in the future. Just look at the console of your daemon and you will know what's the correct ip:port for you.

Cabin answered 16/6, 2021 at 9:6 Comment(1)
This feels like more of a comment than an answer. For future reference.Ronaldronalda

© 2022 - 2024 — McMap. All rights reserved.