1

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 internet gives me a lot of different answers.

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

1 Answers1

0

Below is the correct way to add proxy in chrome ,

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:

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. Try above mentioned approach to set proxy

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • I get another error when trying to connect to wikipedia: `urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=62467): Max retries exceeded with url: /session/8d08a1391f64979088c1e0a83cb674b6/url (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))` Does this mean wikipedia is refusing requests from public, free proxy servers? As my proxy is just scraped from a free proxy website. – Statistiekman Dec 06 '20 at 17:37
  • Add screenshot , errors and all details in that question – PDHide Dec 06 '20 at 17:47
  • https://stackoverflow.com/questions/65172321/failing-to-set-up-proxy-server-in-selenium-for-chrome-python-3-7 – Statistiekman Dec 06 '20 at 19:41