1

import zipfile
import json
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import time

def create_proxy_auth_extension(proxy_host, proxy_port, proxy_user, proxy_pass, plugin_path):
    manifest_json = {
        "name": "Chrome Proxy Auth Extension",
        "version": "1.0",
        "manifest_version": 3,
        "permissions": [
            "proxy", "storage", "tabs", "webRequest", "webRequestBlocking"
        ],
        "host_permissions": ["<all_urls>"],
        "background": {
            "service_worker": "background.js"
        }
    }

    background_js = f"""
chrome.runtime.onInstalled.addListener(() => {{
    chrome.proxy.settings.set({{
        value: {{
            mode: "fixed_servers",
            rules: {{
                singleProxy: {{
                    scheme: "http",
                    host: "{proxy_host}",
                    port: {proxy_port}
                }},
                bypassList: ["localhost"]
            }}
        }},
        scope: "regular"
    }});
}});

chrome.webRequest.onAuthRequired.addListener(
    function(details, callback) {{
        callback({{
            authCredentials: {{
                username: "{proxy_user}",
                password: "{proxy_pass}"
            }}
        }});
    }},
    {{urls: ["<all_urls>"]}},
    ["blocking"]
);
"""
    with zipfile.ZipFile(plugin_path, 'w') as zp:
        zp.writestr("manifest.json", json.dumps(manifest_json))
        zp.writestr("background.js", background_js)

# List of proxies to rotate through
proxies = [
    {"host": "38.153.152.2441", "port": 9594, "user": "pdgbxipq", "pass": "48oxy6sf7yv0"},
    {"host": "86.38.234.176", "port": 6630, "user": "pdgbxipq", "pass": "48oxy6sf7yv0"},
    {"host": "173.211.0.148", "port": 6641, "user": "pdgbxipq", "pass": "48oxy6sf7yv0"},
]

# Loop through each proxy and test it
for idx, proxy in enumerate(proxies):
    ext_file = f"proxy_auth_plugin_{idx}.zip"
    create_proxy_auth_extension(proxy["host"], proxy["port"], proxy["user"], proxy["pass"], ext_file)

    chrome_options = Options()
    chrome_options.add_extension(ext_file)

    print(f"\n🔁 Testing Proxy #{idx + 1} ({proxy['host']}:{proxy['port']})...")

    try:
        driver = webdriver.Chrome(options=chrome_options)
        # driver.get("https://httpbin.io/ip")  run this and below after commenting below one to test the proxy
        driver.get("https://twitter.com/")
        time.sleep(2)
        ip = driver.find_element(By.TAG_NAME, "body").text
        print("🌐 Detected IP:", ip)
    except Exception as e:
        print("❌ Error:", e)
    finally:
        driver.quit()
        os.remove(ext_file)

I am working on a project which involves scraping tweets using selenium and chrome driver. I want to rotate through authenticated proxy ip's and access "twitter.com" but it is not working. If someone look into my code and debug it in which you are accessing twitter.com or https://httpbin.io/ip in selenium through proxy rotation , I would be grateful! Also for authenticated proxies I have created account at webshare as they provie 10 authenticated proxies free and so I am using those proxies for rotation.

I have tried setting up extension in selenium for chrome browser for using authenticated proxy ip but that didnt work as the driver chrome still asked for password and username to enter on popup when visitng a https://httpbin.io/ip and twitter.com didnt work; I will attach my python code below and some screenshots. To run this code you need to create any python file and then run that python file using "py filename.py" or : "python filename.py". Also for authenticated proxies I have created account at webshare as they provie 10 authenticated proxies free and so I am using those proxies for rotation.

1 Answer 1

0

you can use selenium-wire to authenticate private proxies. Use a for loop to test all proxies
https://pypi.org/project/selenium-wire/

pip install selenium-wire
from seleniumwire import webdriver
options = {
    'proxy': {
        'https': 'https://user:[email protected]:8888',
    }
}
driver = webdriver.Chrome(seleniumwire_options=options)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.