Python Selenium 本机开启代理,谷歌浏览器关闭代理登录弹窗配置


from selenium.webdriver.chrome.service import Service

import time
import string
import zipfile
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def create_proxyauth_extension(proxy_host, proxy_port,proxy_username, proxy_password,scheme='http', plugin_path=None):
    """Proxy Auth Extension
    args:
        proxy_host (str): domain or ip address, ie proxy.domain.com
        proxy_port (int): port
        proxy_username (str): auth username
        proxy_password (str): auth password
    kwargs:
        scheme (str): proxy scheme, default http
        plugin_path (str): absolute path of the extension
    return str -> plugin_path
    """
    if plugin_path is None:
        plugin_path = 'Selenium-Chrome-HTTP-Private-Proxy.zip'
    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 = string.Template(
        """
        var config = {
                mode: "fixed_servers",
                rules: {
                  singleProxy: {
                    scheme: "${scheme}",
                    host: "${host}",
                    port: parseInt(${port})
                  },
                  bypassList: ["foobar.com"]
                }
              };
        chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
        function callbackFn(details) {
            return {
                authCredentials: {
                    username: "${username}",
                    password: "${password}"
                }
            };
        }
        chrome.webRequest.onAuthRequired.addListener(
                    callbackFn,
                    {urls: ["<all_urls>"]},
                    ['blocking']
        );
        """
    ).substitute(
        host=proxy_host,
        port=proxy_port,
        username=proxy_username,
        password=proxy_password,
        scheme=scheme,
    )
    with zipfile.ZipFile(plugin_path, 'w') as zp:
        zp.writestr("manifest.json", manifest_json)
        zp.writestr("background.js", background_js)

    return plugin_path

def configure_headless_browser(proxy_config):
    chrome_options = Options()
    chrome_options.add_argument("--start-maximized")
    proxyauth_plugin_path = create_proxyauth_extension(
        proxy_host=proxy_config[0],
        proxy_port=proxy_config[1],
        proxy_username=proxy_config[2],
        proxy_password=proxy_config[3]
    )
    chrome_options.add_extension(proxyauth_plugin_path)
    return webdriver.Chrome(options=chrome_options, service=Service(r'D:\test\chromedriver.exe'))
# 设置代理IP:分别填入代理ip,端口,账号,密码
proxy_config = ["10.10.30.90", "80", "username", "password"]

driver = configure_headless_browser(proxy_config)
driver.get('http://httpbin.org/ip')


可以使用PythonSelenium库来清理谷歌浏览器缓存。具体步骤如下: 1. 首先需要导入Selenium库和webdriver模块,以及ChromeOptions类和Options类。 ```python from selenium import webdriver from selenium.webdriver.chrome.options import Options ``` 2. 创建一个ChromeOptions对象,并设置浏览器参数。 ```python options = Options() options.add_argument('--ignore-certificate-errors') options.add_argument('--ignore-ssl-errors') ``` 3. 创建一个WebDriver对象,并使用ChromeOptions对象作为参数来设置浏览器参数。 ```python driver = webdriver.Chrome(chrome_options=options) ``` 4. 打开浏览器,并访问需要清理缓存的网站。 ```python driver.get('https://www.example.com') ``` 5. 使用execute_script()方法执行JavaScript代码来清理缓存。 ```python driver.execute_script('window.localStorage.clear();') driver.execute_script('window.sessionStorage.clear();') driver.execute_script('window.location.reload();') ``` 6. 关闭浏览器。 ```python driver.quit() ``` 完整的代码示例: ```python from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument('--ignore-certificate-errors') options.add_argument('--ignore-ssl-errors') driver = webdriver.Chrome(chrome_options=options) driver.get('https://www.example.com') driver.execute_script('window.localStorage.clear();') driver.execute_script('window.sessionStorage.clear();') driver.execute_script('window.location.reload();') driver.quit() ``` 注意:这个方法只能清理浏览器缓存,不能清理浏览器历史记录和cookie等其他数据。如果需要清理其他数据,可以使用selenium的delete_all_cookies()方法来删除cookie。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值