通过代理(python)运行Selenium

通过代理(python)运行Selenium,python,selenium,proxy,Python,Selenium,Proxy,我已经设置了一些代码来通过代理列表运行selenium。第一种方法并不真正相关。它只是从.txt文档中获取IP和端口,这样它们就可以通过代理(IP,端口)方法循环 def IP_PORT_select(n): f = open("/home/usr/Desktop/proxies", "r") doc = f.read() IP_PORT = re.compile("\d+") match = IP_PORT.findall(doc) IP = match

我已经设置了一些代码来通过代理列表运行selenium。第一种方法并不真正相关。它只是从.txt文档中获取IP和端口,这样它们就可以通过
代理(IP,端口)
方法循环

def IP_PORT_select(n):
    f = open("/home/usr/Desktop/proxies", "r")
    doc = f.read()
    IP_PORT = re.compile("\d+")
    match = IP_PORT.findall(doc)
    IP = match[n] + "." + match[n+1] + "." + match[n+2] + "." + match[n+3]
    PORT = match[n+4]
    return IP, PORT

def proxy(IP, PORT):
    firefox_profile = webdriver.FirefoxProfile()
    firefox_profile.set_preference("network.proxy.type", 1)
    firefox_profile.set_preference("network.proxy.http", IP)
    firefox_profile.set_preference("network.proxy.http_port", int(PORT))
    firefox_profile.set_preference("network.proxy.share_proxy_settings", True)
    driver = webdriver.Firefox(firefox_profile=firefox_profile)
    return driver
我基本上是从这里的另一个问题复制过来的,但我在最后添加了我自己的部分
firefox\u profile.set\u preference(“network.proxy.share\u proxy\u settings”,True)

我注意到,即使我手动设置了一个代理,它也不会工作,除非我选中了“将此地址用于所有代理”框。现在,当我通过selenium运行测试时,除非我手动进入设置并单击“确定”,否则它不会工作。现在奇怪的是,从技术上讲,所有正确的信息都在那里,只是在我手动检查并“确定”所有信息之前,它不会被激活

顺便说一句,我还遇到了很多错误“代理拒绝连接”(或类似的错误)。我不确定是我做错了什么,还是代理的错。而且,如果有人对清理我的代码有什么建议,我很乐意听听