在Python的Selenium Webdriver中将HTTP代理与无头firefox结合使用 [英] Using a http proxy with headless firefox in Selenium webdriver in Python

查看:91
本文介绍了在Python的Selenium Webdriver中将HTTP代理与无头firefox结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在这样使用无头的Firefox:

I'm using Firefox headless like this:

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium import webdriver
import os
import sys

# Set the MOZ_HEADLESS environment variable which casues 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代理.搜索后,我尝试了以下操作:

But now I want to add a http proxy that requires a user/password. After searching around, I tried the following:

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)

我也尝试过

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)

最后,我尝试将带有凭据的"socksUsername"和"socksPassword"添加到proxy,更多的是出于绝望而不是任何真正的希望.

Finally, I tried adding "socksUsername" and "socksPassword" with the creds to the proxy, more out of desperation than any real hope.

不用说这些都不起作用,并且测试表明请求仍在使用我通常的IP,而不是代理.

Needless to say none of these works, and testing shows requests are still using my usual IP, not the proxy.

在这种情况下,也不选择系统范围的代理.

Also system-wide proxy is not an option in this case.

http代理凭据应放在哪里?如何在无头的Firefox中使用代理?

Where should the http proxy credentials live? How can I use a proxy with headless firefox?

测试

driver.get("https://www.ipinfo.io");
driver.find_element_by_xpath('//h4/following-sibling::p').text

推荐答案

如果您的代理服务器需要用户名和密码,则必须这样写:

If your proxy requires a username and a password, you have to write it like that :

from selenium.webdriver.common.proxy import Proxy, ProxyType

myProxy = "username:password@proxyDomain:proxyPort"

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)

这篇关于在Python的Selenium Webdriver中将HTTP代理与无头firefox结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆