如何为Selenium WebDriver设置Socks5代理?

6
我真的无法为我的Selenium Python Chrome WebDriver设置Socks5代理(包括HTTP...)。我尝试了很多不同的方法...但是我觉得我做错了什么。
self.options.add_argument('--proxy-server=http://'+proxy)

例子2:
webdriver.DesiredCapabilities.CHROME['proxy'] = {
        "socksProxy": proxy,
        "ftpProxy": proxy,
        "sslProxy": proxy,
        "noProxy": None,
        "proxyType": "MANUAL",
        "class": "org.openqa.selenium.Proxy",
        "autodetect": False
    }

请详细描述在Python和Chrome webdriver上设置Selenium的socks5代理的工作示例,包括代理字符串格式的示例(也许我在这里做了一些错误...)。
附注:我遇到了两个问题:
1. 只保留旧的IP地址。 2. Chrome web driver没有网络连接。
7个回答

14
Chrome不允许带有身份验证的代理。我不确定,但经过详细阅读后我认为是这样... 目前我只有一个解决方案。我正在使用一个没有登录和密码身份验证的SOCKS5代理。
options = webdriver.ChromeOptions()
proxy = '12.12.421.125:1949'
options.add_argument('--proxy-server=socks5://' + proxy)
driver = webdriver.Chrome(options=options)

2023编辑

根据 man chromium 的说法,支持这个论点:

--proxy-server=host:port
指定用于请求的HTTP/SOCKS4/SOCKS5代理服务器。这将覆盖任何环境变量或通过选项对话框选择的设置。


现在我们能否使用 Chrome 进行 Provy 认证? - 0xTheOldOne
我刚刚在Windows上使用Chrome版本116.0.5845.97(官方构建)(64位)和我的自定义代理服务器进行了一次测试。目前,Chrome仅支持“无需身份验证”的认证方法。 - Peter Bergman

3
如果您只想为 FireFox 的 geckodriver 设置 socks5 代理主机/socks5 代理,请按如下方式操作:
from selenium import webdriver

profile = webdriver.FirefoxProfile()

# Socks5 Host SetUp:-
myProxy = "198.199.101.152:8388"
ip, port = myProxy.split(':')
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', ip)
profile.set_preference('network.proxy.socks_port', int(port))

driver = webdriver.Firefox(firefox_profile=profile)

1
这是我用来连接带有用户名/密码验证的Socks5服务器的代码。
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

capabilities = dict(DesiredCapabilities.CHROME)
capabilities['proxy'] = {
    'proxyType': 'MANUAL',
    'socksProxy': '<Your_IP>:<Your_Port>',
    'socksVersion': 5,
    'ftpProxy': '<Your_IP>:<Your_Port>',
    'noProxy': 'localhost,127.0.0.1',
    'class': "org.openqa.selenium.Proxy",
    'autodetect': False
}

capabilities['proxy']['socksUsername'] = '<username>'
capabilities['proxy']['socksPassword'] = '<password>'

driver = Chrome(ChromeDriverManager().install(), desired_capabilities=capabilities)

它不起作用,有任何更新吗? - 4rigener
我尝试了这个,最后一行我不得不改成“driver = webdriver.Chrome(...”才能让它工作,但是一旦浏览器打开,并尝试进入google.com,我收到了一个错误:“ERR_SOCKS_CONNECTION_FAILED” 我正在使用我的NordVPN登录和此代理服务器:atlanta.us.socks.nordhold.net:1080 - Steven

1

这段代码对我有效。我在谷歌上搜索了很多关于在Chrome中设置socks5代理的内容。但对于像我这样的新手来说,这并不是一项容易的任务。在Firefox中设置socks5代理要简单得多。这段代码对我有效。但问题在于SSL。我仍然不知道如何解决这个问题。但这段代码可能对像我这样的人有帮助,所以我在这里发布它。

import undetected_chromedriver as webdriver
from webdriver_manager.chrome import ChromeDriverManager
from seleniumwire import webdriver as sw_webdriver



ip_checker = 'https://whoer.net'
        url = 'https://myaccount.google.com/signinoptions/password?rapt='
        p_list = random.choice(prox())
        proxy_options = {
            'proxy': {
                'http': f'socks5://{p_list}',
                'https': f'socks5://{p_list}',
                'no_proxy': 'localhost,127.0.0.1'  # add any exceptions here
            }
        }


# CHROME SETTINGS 3
    

options = webdriver.ChromeOptions()
        options.add_argument("--log-level=3")
        options.add_argument(r"user-data-dir=C:\Users\iron_\AppData\Local\Google\Chrome Beta\User Data")
        options.binary_location = r"C:\Program Files\Google\Chrome Beta\Application\chrome.exe"

        # Set proxy options using selenium-wire
        sw_options = {
            'proxy': {
                'http': proxy_options['proxy']['http'],
                'https': proxy_options['proxy']['https'],
                'no_proxy': proxy_options['proxy']['no_proxy'],
            }
        }

    

# Create the Chrome web driver with the specified options and proxy settings
        driver = sw_webdriver.Chrome(
            executable_path=ChromeDriverManager().install(),
            options=options,
            seleniumwire_options=sw_options
        )
        driver.get(ip_checker)

0

关于 Firefox的geckodriver,根据Tanmay Harsh的答案,我已经得到了以下内容:

from selenium.webdriver import Firefox, FirefoxOptions

proxy = ('proxy-server.local', 1080)

options = FirefoxOptions()
options.set_preference('network.proxy.type', 1)
options.set_preference('network.proxy.socks', proxy[0])
options.set_preference('network.proxy.socks_port', proxy[1])
options.set_preference('network.proxy.socks_remote_dns', True)
driver = Firefox(options=options)

0
解决方案已在2023年版本进行测试,使用IPv6代理(无需selenium-wire)。CHROME浏览器:
options.add_argument("--ignore-certificate-errors")
options.set_capability('acceptInsecureCerts', True)

host = "xx"
port = "xx"
socks5User = "xx"
socks5Pass = "xx"
options.add_argument("--proxy-server=socks5://" + host + ":" + port)
options.add_argument("--proxy-auth=" + socks5User + ":" + socks5Pass)
driver = webdriver.Chrome(options=options)

-3
options = {
    'proxy': {
        'http': 'socks5://user:pass@192.168.10.100:8888',
        'https': 'socks5://user:pass@192.168.10.100:8888',
        'no_proxy': 'localhost,127.0.0.1'
    }
}
driver = webdriver.Chrome(seleniumwire_options=options)

源代码 - https://github.com/wkeeling/selenium-wire#socks

你的回答可以通过添加更多支持性信息来改进。请[编辑]以添加更多细节,例如引用或文档,以便其他人可以确认您的答案是否正确。您可以在帮助中心找到有关编写好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接