[英]Failing to set up proxy for chrome in Selenium (Python 3.7)
When running the following program, the following error pops up:运行以下程序时,弹出如下错误:
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: cannot parse capability: proxy from invalid argument: unrecognized proxy type: unspecified
I can't figure out what I'm doing wrong.我不知道我做错了什么。 I think it has something to do with setting up the chrome webdriver but I have no idea how to correctly do this as the inte.net gives me a lot of different answers.我认为这与设置 chrome webdriver 有关,但我不知道如何正确执行此操作,因为 inte.net 给了我很多不同的答案。
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
import random
from string import ascii_lowercase
PATH = r"C:\Users\...\PythonProject\chromedriver.exe"
def proxy_update():
global driver
proxies = []
driver.get('https://free-proxy-list.net/')
proxy_search = driver.find_element_by_xpath("""//*[@id="proxylisttable"]/tbody""").text.split('ago\n')
for pr in proxy_search:
proxies.append(pr.split(' '))
for lst in proxies:
for pr in lst:
if pr == "elite":
proxies.append(lst[0] + ':' + lst[1])
else:
pass
if len(proxies) > 20:
driver.quit()
return random.sample(proxies, k=1)
else:
proxy_update()
capabilities = webdriver.DesiredCapabilities.CHROME.copy()
prox = Proxy()
prox.add_to_capabilities(capabilities)
prox.proxy_type = ProxyType.MANUAL
driver = webdriver.Chrome(PATH, desired_capabilities=capabilities)
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(f'--proxy-server=http://{proxy_update()}')
driver = webdriver.Chrome(PATH, desired_capabilities=capabilities, options=chrome_options)
prox.http_proxy = f"{proxy_update()}"
prox.socks_proxy = f"{proxy_update()}"
prox.ssl_proxy = f"{proxy_update()}"
proxy_update()
driver.get("https://www.wikipedia.org/wiki/Rotterdam")
This is the full error:这是完整的错误:
Traceback (most recent call last):
File "C:/Users/.../PythonProject.py", line 36, in <module>
driver = webdriver.Chrome(PATH, desired_capabilities=capabilities)
File "C:\Users\...\venv\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "C:\Users\...\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\...\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\...\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\...\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: cannot parse capability: proxy
from invalid argument: unrecognized proxy type: unspecified
Below is the correct way to add proxy in chrome,以下是在 chrome 中添加代理的正确方法,
JAVA: JAVA:
ChromeOptions chromeOptions = new ChromeOptions();
String proxyadd = "176.9.119.170:8080";
Proxy proxy = new Proxy();
proxy.setHttpProxy(proxyadd);
proxy.setSslProxy(proxyadd);
chromeOptions.setCapability("proxy", proxy);
WebDriver driver = new ChromeDriver(chromeOptions);
PYTHON: PYTHON:
from selenium import webdriver
PROXY="176.9.119.170:8080"
webdriver.DesiredCapabilities.CHROME['proxy'] = {
"httpProxy": PROXY,
"ftpProxy": PROXY,
"sslProxy": PROXY,
"proxyType": "MANUAL",
}
webdriver.DesiredCapabilities.CHROME['acceptSslCerts']=True
driver =webdriver.Chrome(r".\chromedriver.exe")
driver.get("https://www.google.com")
It seems that chrome is not able to connect to proxy may be it is using system proxy. chrome 似乎无法连接到代理,可能是它正在使用系统代理。 Try above mentioned approach to set proxy试试上面提到的设置代理的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.