问题
I've done plenty of searching however there is a lot of confusing snippets out there that are very similar.
I've attempted to use the DesiredCapabilities
, ChromeOptions
, Options
and a series of arguments but nothing is working :( It fails to set a proxy.
For example (ChromeOptions
)
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy=https://' + proxy_ip_and_port)
chrome_options.add_argument('--proxy-auth=' + proxy_user_and_pass)
chrome_options.add_argument('--proxy-type=https')
browser = webdriver.Chrome("C:\drivers\chromedriver.exe")
Another example (Options
)
options = Options()
options.add_argument('--proxy=https://' + proxy_ip_and_port)
options.add_argument('--proxy-auth=' + proxy_user_and_pass)
options.add_argument('--proxy-type=https')
browser = webdriver.Chrome("C:\drivers\chromedriver.exe", chrome_options=options)
I've also used --proxy-server
instead of --proxy-auth
, --proxy-type
... etc even in the format of: '--proxy-server=http://' + proxy_user_and_pass + '@' + proxy_ip_and_port
Another example (DesiredCapabilities
)
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
capabilities = dict(DesiredCapabilities.CHROME)
capabilities['proxy'] = {'proxyType': 'MANUAL',
'httpProxy': proxy_ip_and_port,
'ftpProxy': proxy_ip_and_port,
'sslProxy': proxy_ip_and_port,
'noProxy': '',
'class': "org.openqa.selenium.Proxy",
'autodetect': False}
capabilities['proxy']['socksUsername'] = proxy_user
capabilities['proxy']['socksPassword'] = proxy_pass
browser = webdriver.Chrome(executable_path="C:\drivers\chromedriver.exe", desired_capabilities=capabilities)
I've tried in Firefox too but the same issue happens, it uses the browser with my normal IP.
回答1:
According to the latest documentation (Jul 2020) you set the DesiredCapabilities
for either FIREFOX
or CHROME
.
I've tested it for Firefox. You can check your browser's connection settings afterwards to validate the proxy was set correctly.
from selenium import webdriver
PROXY = "<HOST>:<PORT>" # HOST can be IP or name
webdriver.DesiredCapabilities.FIREFOX['proxy'] = {
"httpProxy": PROXY,
"ftpProxy": PROXY,
"sslProxy": PROXY, # this is the https proxy
"proxyType": "MANUAL",
}
with webdriver.Firefox() as driver:
# Open URL
driver.get("https://selenium.dev")
The proxy-dict itself is documented in the Selenium wiki. You'll see here that the attribute sslProxy
sets the proxy for https.
I haven't tested it for Chrome though. If it shouldn't work, you may find clues in Google's ChromeDriver documentation. According to this you also need to instantiate the webdriver with the desired_capabilites
parameter (which is then actually very similar to your example, so this is now more of a guess than a proven solution):
caps = webdriver.DesiredCapabilities.CHROME.copy()
caps['proxy'] = ... # like described above
driver = webdriver.Chrome(desired_capabilities=caps)
driver.get("https://selenium.dev")
来源:https://stackoverflow.com/questions/49405337/selenium-chrome-firefox-webdriver-set-https-proxy-in-python