简体   繁体   English

在Python的Selenium webdriver中使用带有无头firefox的http代理

[英]Using a http proxy with headless firefox in Selenium webdriver in Python

I'm using Firefox headless like this: 我正在使用像这样的Firefox无头:

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)

But now I want to add a http proxy that requires a user/password. 但现在我想添加一个需要用户/密码的http代理。 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)

I also tried 我也试过了

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)

Finally, I tried adding "socksUsername" and "socksPassword" with the creds to the proxy , more out of desperation than any real hope. 最后,我尝试将“socksUsername”和“socksPassword”添加到proxy信用,更多的是绝望而不是任何真正的希望。

Needless to say none of these works, and testing shows requests are still using my usual IP, not the proxy. 不用说,这些都不起作用,测试显示请求仍然使用我常用的IP,而不是代理。

Also system-wide proxy is not an option in this case. 在这种情况下,系统范围的代理也不是一个选项。

Where should the http proxy credentials live? http代理凭证应该在哪里? How can I use a proxy with headless firefox? 如何使用无头firefox的代理?

Testing 测试

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)

Have you try setting up a profile manually with 您是否尝试手动设置配置文件?

./firefox --ProfileManager

manually setup the proxy and then load the profile you manually set 手动设置代理,然后加载手动设置的配置文件

from selenium import webdriver

url = "https://mail.google.com"
fp = webdriver.FirefoxProfile('/Users/<username>/Library/Application Support/Firefox/Profiles/71v1uczn.default')

driver = webdriver.Firefox(fp)

You can try setting up an Environment variable " HTTP_PROXY " in following mnemonics: 您可以尝试在以下助记符中设置环境变量“ HTTP_PROXY ”:

http://<username>:<password>@<proxy_url>

Add your credentials separated by colon ':' before proxy url that is preceded by '@' for eg 在代理URL之前添加以冒号':'分隔的凭据,例如前面带有'@'

http://username:password@proxy.com:8080/file.pac

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 selenium webdriver在带firefox浏览器的无头Linux上使用python在下拉菜单中选择一个选项 - selenium webdriver select an option in dropdown menu using python on headless Linux with firefox browser Selenium 使用 Python:为 firefox 输入/提供 http 代理密码 - Selenium using Python: enter/provide http proxy password for firefox 使用 Firefox 无头、Selenium 和 Python 时出错 - Error while using Firefox headless, Selenium and Python Selenium Webdriver Firefox 52 Python 每次运行选择随机代理 - Selenium Webdriver Firefox 52 Python select random proxy every run Selenium Chrome 和 Firefox WebDriver:在 Python 中设置 HTTPS 代理 - Selenium Chrome & Firefox WebDriver: Set HTTPS proxy in Python 无头和代理身份验证 Selenium Python - Headless and Proxy authentication Selenium Python Python 在 Selenium 和 Firefox Webdriver 中使用 Adblock - Python Using Adblock with Selenium and Firefox Webdriver 在Python中使用Selenium Webdriver在后端中运行Firefox - Run Firefox in Backend using Selenium webdriver in Python selenium webdriver sendkeys()使用python和firefox - selenium webdriver sendkeys() using python and firefox 在python中使用默认的firefox配置文件和selenium webdriver - Using the default firefox profile with selenium webdriver in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM