如何在Python Selenium无头Firefox中配置带账号密码的HTTP代理?
无头Firefox配置带用户名密码的HTTP代理问题
我目前用下面这段代码启动无头Firefox:
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary from selenium import webdriver import os import sys # Set the MOZ_HEADLESS environment variable which causes Firefox to # start in headless mode. os.environ['MOZ_HEADLESS'] = '1' # Select your Firefox binary. binary = FirefoxBinary('/usr/bin/firefox', log_file=sys.stdout) # Start selenium with the configured binary. driver = webdriver.Firefox(firefox_binary=binary)
现在我需要给它加上一个需要用户名/密码验证的HTTP代理,搜索后试了几种方法,但都没效果——测试显示请求还是用我的本地IP,完全没走代理,而且系统级代理我这边没法用。
我试过的方法如下:
方法一:用Proxy类配置
from selenium.webdriver.common.proxy import Proxy, ProxyType myProxy = "xx.xx.xx.xx:80" proxy = Proxy({ 'proxyType': ProxyType.MANUAL, 'httpProxy': myProxy, 'ftpProxy': myProxy, 'sslProxy': myProxy, 'noProxy': '' # set this value as desired }) driver = webdriver.Firefox(firefox_binary=binary, proxy=proxy)
方法二:通过FirefoxProfile配置
profile = webdriver.FirefoxProfile() profile.set_preference("network.proxy.type", 1) profile.set_preference("network.proxy.http", "xx.xx.xx.xx") profile.set_preference("network.proxy.http_port", 80) profile.update_preferences() driver=webdriver.Firefox(firefox_binary=binary,firefox_profile=profile)
另外我还抱着试试的心态,在Proxy的参数里加了socksUsername和socksPassword字段填凭证,但显然不对,还是没用。
请问这种需要用户名密码的HTTP代理,凭证应该配置在哪里?到底怎么才能在无头Firefox里正确用上代理?
我用来测试的代码是:
driver.get("https://www.ipinfo.io"); driver.find_element_by_xpath('//h4/following-sibling::p').text
内容的提问来源于stack exchange,提问作者fpghost
相关产品推荐
相关产品推荐

