how can i configurate proxy in selenium python, I am using the last version of python and selenium I am tryng that
proxy = Proxy({"proxyType": ProxyType.AUTODETECT})
client_config = ClientConfig(remote_server_addr=grid_server,
proxy=proxy,
timeout=3600,
ignore_certificates=True,
username="admin", password="myStrongPassword")
options = get_default_chrome_options()
driver = webdriver.Remote(command_executor=grid_server, options=options, client_config=client_config)
driver.get(";)
driver.quit()
but i dont know how can i put the data of proxy
i don't know that put in remote_server_addr=grid_server ,proxy=proxy,username="admin", password="myStrongPassword"
how can i configurate proxy in selenium python, I am using the last version of python and selenium I am tryng that
proxy = Proxy({"proxyType": ProxyType.AUTODETECT})
client_config = ClientConfig(remote_server_addr=grid_server,
proxy=proxy,
timeout=3600,
ignore_certificates=True,
username="admin", password="myStrongPassword")
options = get_default_chrome_options()
driver = webdriver.Remote(command_executor=grid_server, options=options, client_config=client_config)
driver.get("https://www.selenium.dev")
driver.quit()
but i dont know how can i put the data of proxy
i don't know that put in remote_server_addr=grid_server ,proxy=proxy,username="admin", password="myStrongPassword"
- Whats the Selenium version can you add information about versions. by latest selenium 4.29.0 would it be correct – Jeet Singh Commented Mar 18 at 15:17
- I am using selenium 4.29.0. how can i put o configurate my proxy with thats code – Hee Mi Commented Mar 18 at 15:21
- Is is an authenticated proxy? – Jeet Singh Commented Mar 18 at 15:28
- yes. is an authenticated proxy – Hee Mi Commented Mar 18 at 15:31
2 Answers
Reset to default 1You’re trying to configure a proxy with authentication in Selenium using a Remote WebDriver (Selenium Grid), but the challenge is that Selenium’s --proxy-server argument does not support username and password authentication by default.
Option 1
You need to bypass this by using methods like Selenium Wire
https://www.zenrows/blog/selenium-proxy#proxy-authentication-selenium
PS: Its no longer maintained but i'm not sure about an alternative
Option 2
Create a Chrome Extension to inject proxy authentication.
Step 1 :create an script to create the extension
import zipfile
PROXY_HOST = "proxyserver"
PROXY_PORT = "8080"
PROXY_USER = "admin"
PROXY_PASS = "myStrongPassword"
def create_proxy_extension():
manifest_json = """{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": ["proxy", "webRequest", "webRequestBlocking", "<all_urls>"],
"background": {"scripts": ["background.js"]}
}"""
background_js = f"""
var config = {{
mode: "fixed_servers",
rules: {{
singleProxy: {{
scheme: "http",
host: "{PROXY_HOST}",
port: parseInt({PROXY_PORT})
}},
bypassList: ["localhost"]
}}
}};
chrome.proxy.settings.set({{value: config, scope: "regular"}}, function() {{}});
chrome.webRequest.onAuthRequired.addListener(
function(details) {{
return {{
authCredentials: {{
username: "{PROXY_USER}",
password: "{PROXY_PASS}"
}}
}};
}},
{{urls: ["<all_urls>"]}},
["blocking"]
);
"""
with zipfile.ZipFile("proxy_auth_plugin.zip", "w") as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
create_proxy_extension()
Step 2
load the extension into Selenium Chrome WebDriver:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_extension("proxy_auth_plugin.zip")
driver = webdriver.Chrome(service=Service("/path/to/chromedriver"), options=chrome_options)
driver.get("https://www.selenium.dev")
driver.quit()
ChromeOptions has an --proxy-server
option:
https://developer.chrome/docs/chromedriver/capabilities
#!/usr/bin/env python3
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
options = webdriver.ChromeOptions()
# profile where you have predefined all your stuff
#options.add_argument('user-data-dir=C:\\Users\\mobj\\AppData\\Local\\Google\\Chrome\\User Data\\Default')
# if not using a predefined profile you might need the following
#options.add_argument("--incognito")
#options.add_argument("--disable-search-engine-choice-screen")
# for proxy
#proxy = "proxy.XXXX.YYYY:3128"
#options.add_argument(f"--proxy-server={proxy}")
# do not need path if chromedriver if its in your PATH
service = ChromeService(service_args=['C:\\dist\\webdrivers\\chromedriver.exe', "--verbose", "--log-path=./chromedriver.log"])
driver = webdriver.Chrome(service=service, options=options)
driver.get('https://www.google.no')
# just to keep the window open
time.sleep(100000)