硒代理设置

时间:2019-06-25 12:36:07

标签: python selenium proxy

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

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")

1 个答案:

答案 0 :(得分:0)

定义您的属性:

    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/")