首页 > 解决方案 > Selenium 代理设置

问题描述

我需要在 Python 中通过代理使用 Selenium。我的私人代理类型是:user:password:ip:port. 下面的代码有效,但它没有userandpassword选项。有人可以更新此代码吗?谢谢。

from selenium import webdriver
import time


"Define Both ProxyHost and ProxyPort as String"
ProxyHost = "ip"
ProxyPort = "port"



def ChangeProxy(ProxyHost ,ProxyPort):
    "Define Firefox Profile with you ProxyHost and ProxyPort"
    profile = webdriver.FirefoxProfile()
    profile.set_preference("network.proxy.type", 1)
    profile.set_preference("network.proxy.http", ProxyHost )
    profile.set_preference("network.proxy.http_port", int(ProxyPort))
    profile.update_preferences()
    return webdriver.Firefox(firefox_profile=profile)


def FixProxy():
    "Reset Firefox Profile"
    profile = webdriver.FirefoxProfile()
    profile.set_preference("network.proxy.type", 0)
    return webdriver.Firefox(firefox_profile=profile)


driver = ChangeProxy(ProxyHost ,ProxyPort)
driver.get("http://whatismyipaddress.com")

time.sleep(5)

driver = FixProxy()
driver.get("http://whatismyipaddress.com")

标签: pythonseleniumproxy

解决方案


定义你的属性:

    username = "your_username_here" 
    password = "your_password_here"

然后将这些添加到您的偏好中:

    profile.set_preference("network.proxy.socks_username", username)
    profile.set_preference("network.proxy.socks_password", password)

另一种方法:

profile.set_preference('network.http.phishy-userpass-length', 255)
driver = webdriver.Firefox(firefox_profile=profile)
driver.get("https://username:password@somewebsite.com:port/")

推荐阅读