问题描述
我正在寻找可以帮助我的答案......我有一个项目,我需要使用用户名和密码连接到代理。我正在关注以下内容 来自stackoverflow的答案
Python proxy authentication through Selenium chromedriver
但是当我运行我的代码时,selenium 没有告诉我任何信息,并且没有设置用户名和密码。这是使用 add_extension
方法。
我也尝试过 install_extension
方法,但我对这个方法没有任何运气...这是我的代码...我希望有人能帮助我。
Taskscheduler.py
from webdriver.driver import RunSelenium
class TaskScheduler:
driver = driver_setup()
TaskScheduler()
驱动程序.py
from selenium import webdriver
from selenium.webdriver.support.ui import webdriverwait
from .proxy import inject_proxy_extension
from selenium.webdriver.firefox.options import Options
from selenium.webdriver import DesiredCapabilities
from time import sleep
class RunSelenium:
@staticmethod
def get_firefox_profile(proxy_host=None,proxy_port=None,fp=None):
# Direct = 0,Manual = 1,PAC = 2,AUTODETECT = 4,SYstem = 5
if proxy_host is not None:
fp.set_preference("network.proxy.type",1)
fp.set_preference("network.proxy.http",proxy_host)
fp.set_preference("network.proxy.http_port",proxy_port)
fp.set_preference("network.proxy.ssl",proxy_host)
fp.set_preference("network.proxy.ssl_port",proxy_port)
fp.set_preference("network.proxy.no_proxies_on","localhost,127.0.0.1,*.mozilla.com")
fp.set_preference("browser.link.open_newwindow",1)
fp.set_preference("browser.link.open_newwindow.restriction",2)
fp.set_preference('media.mediasource.enabled',True)
fp.set_preference("network.http.pipelining",True)
fp.set_preference("network.http.proxy.pipelining",True)
fp.set_preference("network.http.pipelining.maxrequests",2)
fp.set_preference("network.http.pipelining.ssl",False)
# disable mixed content blocks
fp.set_preference('security.mixed_content.block_active_content',False)
fp.set_preference('security.mixed_content.block_display_content',False)
fp.set_preference("security.OCSP.enabled",False)
fp.set_preference("browser.cache.disk.capacity",0)
fp.set_preference("browser.sessionstore.interval",900000)
fp.set_preference("browser.cache.disk.enable",False)
fp.set_preference("content.interrupt.parsing",False)
fp.set_preference("dom.webnotifications.enabled",False)
fp.set_preference("dom.webnotifications.serviceworker.enabled",False)
fp.set_preference("dom.pushconnection.enabled",False)
fp.set_preference("dom.push.enabled",False)
fp.set_preference("services.sync.prefs.sync.dom.webnotifications.enabled",False)
fp.set_preference("services.sync.prefs.sync.dom.webnotifications.serviceworker.enabled",False)
fp.set_preference("services.sync.prefs.sync.dom.pushconnection.enabled",False)
fp.set_preference("services.sync.prefs.sync.dom.push.enabled",False)
fp.set_preference("browser.privatebrowsing.autostart",True)
fp.set_preference('intl.accept_languages','en-US,en')
fp.set_preference("marionette",False)
fp.set_preference("xpinstall.signatures.required",False);
return fp
@staticmethod
def driver_setup(proxy=None):
headless = False
try:
firefox_profile = webdriver.FirefoxProfile()
capabilities = DesiredCapabilities.FIREFOX
capabilities['overlappingCheckdisabled'] = True
capabilities['unexpecteDalertBehavIoUr'] = 'accept'
if proxy is not None:
firefox_profile = RunSelenium.get_firefox_profile(proxy_host=proxy['hostname'],proxy_port=proxy['port'],fp=firefox_profile)
else:
firefox_profile = RunSelenium.get_firefox_profile(fp=firefox_profile)
opts = Options()
if proxy is not None:
print('go to inject proxy')
firefox_profile = inject_proxy_extension(firefox_profile,proxy['username'],proxy['password'])
opts.headless = headless
#opts.profile = firefox_profile
capabilities.update(opts.to_capabilities())
#firefox_profile.update_preferences()
print("starting Chrome")
print(capabilities)
sleep (5)
# driver = webdriver.Remote(command_executor='http://localhost:4444',desired_capabilities=capabilities)
driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',desired_capabilities=capabilities,browser_profile=firefox_profile)
print("set firefox")
#driver.wait = webdriverwait(driver,100)
return driver
except Exception as e:
print(e)
raise e
代理.py
from selenium.webdriver import DesiredCapabilities,FirefoxProfile
from selenium.webdriver.firefox.options import Options
from selenium import webdriver
import shutil
import fileinput
import os
from selenium import webdriver
def inject_proxy_extension(firefox_profile,proxy_user,proxy_password):}
ROOT_DIR = os.path.abspath(os.curdir)
script_path = os.path.join(ROOT_DIR,"webdriver","external","firefox_proxy_addon","background.js")
script_backup_path = os.path.join(ROOT_DIR,"background.js.bak")
extension_path = os.path.join(ROOT_DIR,"proxyauth")
shutil.copy(script_path,script_backup_path)
try:
os.remove(extension_path + ".zip.xpi")
except OSError:
pass
for line in fileinput.input(script_path,inplace=True):
print(line.replace("PROXY_AUTH_USERNAME",proxy_user)),for line in fileinput.input(script_path,inplace=True):
print(line.replace("PROXY_AUTH_PASS",proxy_password.replace('"','\\"'))),extension_source_path = os.path.join(ROOT_DIR,"firefox_proxy_addon")
shutil.make_archive(extension_path,'zip',extension_source_path)
shutil.move(extension_path + ".zip",extension_path + ".zip.xpi")
shutil.copy(script_backup_path,script_path)
os.remove(script_backup_path)
file_extension = os.path.join(os.getcwd(),extension_path + ".zip.xpi")
firefox_profile._install_extension(os.path.join(extension_path + ".zip.xpi"),unpack=True)
firefox_profile.set_preference("extensions.logging.enabled",True)
return firefox_profile
manifest.json
{
"description": "HTTP Basic Poxy Auth","manifest_version": 2,"name": "HTTP Basic Poxy Auth","version": "1.0","permissions": [
"webRequest","webRequestBlocking","activeTab","<all_urls>"
],"background": {
"scripts": ["background.js"]
}
}
背景.js
{
"description": "HTTP Basic Poxy Auth","background": {
"scripts": ["background.js"]
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)