Headless Selenium不适用于代理(python)

Headless Selenium不适用于代理(python),python,selenium,selenium-webdriver,proxy,Python,Selenium,Selenium Webdriver,Proxy,我想做什么:使用脚本,使用Selenium和经过身份验证的代理单击网页上的按钮 我的问题:虽然我能够使用无头浏览器和简单的http代理来完成我的任务,但我确实无法使用经过身份验证的user:pass代理使无头工作。如果我在没有headless参数的情况下运行脚本,它确实可以工作 你能帮我解决这个问题吗 这是我的密码: from selenium import webdriver from selenium.webdriver.chrome.options import Options from

我想做什么:使用脚本,使用Selenium和经过身份验证的代理单击网页上的按钮

我的问题:虽然我能够使用无头浏览器和简单的http代理来完成我的任务,但我确实无法使用经过身份验证的user:pass代理使无头工作。如果我在没有headless参数的情况下运行脚本,它确实可以工作

你能帮我解决这个问题吗

这是我的密码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import time
from datetime import datetime
import pickle
from random_user_agent.user_agent import UserAgent
from random_user_agent.params import SoftwareName, OperatingSystem
import random
import os
import zipfile


count = 0

while count < 1000:

    try:

        print("STARTED")
        count += 1

        PROXY_HOST = '212.83.150.248'  # rotating proxy or host
        PROXY_PORT = 5836 # port
        PROXY_USER = 'user-IGNIpuqssk-country-gb' # username
        PROXY_PASS = 'Ikfsvqkdfh1' # password


        manifest_json = """
        {
            "version": "1.0.0",
            "manifest_version": 2,
            "name": "Chrome Proxy",
            "permissions": [
                "proxy",
                "tabs",
                "unlimitedStorage",
                "storage",
                "<all_urls>",
                "webRequest",
                "webRequestBlocking"
            ],
            "background": {
                "scripts": ["background.js"]
            },
            "minimum_chrome_version":"22.0.0"
        }
        """

        background_js = """
        var config = {
                mode: "fixed_servers",
                rules: {
                singleProxy: {
                    scheme: "http",
                    host: "%s",
                    port: parseInt(%s)
                },
                bypassList: ["localhost"]
                }
            };

        chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

        function callbackFn(details) {
            return {
                authCredentials: {
                    username: "%s",
                    password: "%s"
                }
            };
        }

        chrome.webRequest.onAuthRequired.addListener(
                    callbackFn,
                    {urls: ["<all_urls>"]},
                    ['blocking']
        );
        """ % (PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS)

        def get_chromedriver(use_proxy=True, user_agent=True):

            software_names = [SoftwareName.CHROME.value]
            operating_systems = [OperatingSystem.WINDOWS.value, OperatingSystem.LINUX.value]   

            user_agent_rotator = UserAgent(software_names=software_names, operating_systems=operating_systems, limit=10000)
            user_agen = user_agent_rotator.get_random_user_agent()

            path = os.path.dirname(os.path.abspath("/Users/janjan/Desktop/mockupWebsite"))
            chrome_options = webdriver.ChromeOptions()
            chrome_options.headless = True
            if use_proxy:
                pluginfile = 'proxy_auth_plugin.zip'

                with zipfile.ZipFile(pluginfile, 'w') as zp:
                    zp.writestr("manifest.json", manifest_json)
                    zp.writestr("background.js", background_js)
                chrome_options.add_extension(pluginfile)
            if user_agent:
                chrome_options.add_argument(user_agen)
            driver = webdriver.Chrome(
            os.path.join(path, 'chromedriver'),
            options=chrome_options)
            return driver


        driver = get_chromedriver(use_proxy=True)
        driver.get("https://events.kultur-kreativpiloten.de/voting/?projekt_name=lifit")
        elem = driver.find_element_by_class_name('default-like')
        driver.execute_script("arguments[0].click();", elem)
        print(datetime.now(), "TASK NUMBER", count, "DONE")
        driver.close()
    except:
        pass
从selenium导入webdriver
从selenium.webdriver.chrome.options导入选项
从selenium.webdriver.common.proxy导入代理,ProxyType
从selenium.webdriver.common.by导入
从selenium.webdriver.support.ui导入WebDriverWait
导入时间
从日期时间导入日期时间
进口泡菜
从随机用户代理。用户代理导入用户代理
从random_user_agent.params导入SoftwareName、OperatingSystem
随机输入
导入操作系统
进口拉链
计数=0
当计数小于1000时:
尝试:
打印(“开始”)
计数+=1
PROXY_HOST='212.83.150.248'#旋转代理或主机
代理_端口=5836#端口
PROXY_USER='USER IGNIpuqssk country gb'#用户名
PROXY_PASS='Ikfsvqkdfh1'#密码
manifest_json=“”
{
“版本”:“1.0.0”,
“清单版本”:2,
“名称”:“Chrome代理”,
“权限”:[
“代理”,
“标签”,
“无限制存储”,
“存储”,
"",
“网络请求”,
“webRequestBlocking”
],
“背景”:{
“脚本”:[“background.js”]
},
“最低chrome版本”:“22.0.0”
}
"""
背景_js=“”
变量配置={
模式:“固定服务器”,
规则:{
单一代理:{
方案:“http”,
主机:“%s”,
端口:parseInt(%s)
},
旁路列表:[“本地主机”]
}
};
chrome.proxy.settings.set({value:config,scope:“regular”},function(){});
函数callbackFn(详细信息){
返回{
认证证书:{
用户名:“%s”,
密码:“%s”
}
};
}
chrome.webRequest.onAuthRequired.addListener(
Callbackbon,
{URL:[“”]},
[“阻塞”]
);
“”“%(代理主机、代理端口、代理用户、代理通行证)
def get_chromedriver(使用代理=True,用户代理=True):
软件名称=[SoftwareName.CHROME.value]
operating_systems=[OperatingSystem.WINDOWS.value,OperatingSystem.LINUX.value]
用户代理旋转器=用户代理(软件名称=软件名称,操作系统=操作系统,限制=10000)
user\u agen=user\u agent\u rotator.get\u random\u user\u agent()
path=os.path.dirname(os.path.abspath(“/Users/janjan/Desktop/mockupWebsite”))
chrome\u options=webdriver.ChromeOptions()
chrome_options.headless=真
如果使用_代理:
pluginfile='proxy\u auth\u plugin.zip'
zipfile.zipfile(pluginfile,'w')作为zp:
writestr(“manifest.json”,manifest\u json)
zp.writestr(“background.js”,background_js)
chrome\u选项。添加\u扩展(pluginfile)
如果用户/代理:
chrome\u选项。添加参数(用户代理)
driver=webdriver.Chrome(
join(路径'chromedriver'),
选项=chrome\u选项)
返回驱动器
driver=get\u chromedriver(使用\u proxy=True)
驱动程序。获取(“https://events.kultur-kreativpiloten.de/voting/?projekt_name=lifit")
elem=driver.find\u element\u by\u class\u name('default-like'))
驱动程序。执行_脚本(“参数[0]。单击();”,elem)
打印(datetime.now(),“任务编号”,计数,“完成”)
驱动程序关闭()
除:
通过