Failing to set up proxy for chrome in Selenium (Python 3.7)

Statistiekman

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
PDHide

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to set proxy for Chrome browser in selenium using Java code

Set proxy server with selenium and Chrome

Set up Python 3 build system with Sublime Text 3

How to set Proxy setting for Chrome in Selenium Java

Selenium opens up chrome not signed into Chrome account

Set up tests with PHPUnit and Selenium

Set up a proxy with python requests

set proxy address with selenium chrome

Disable proxy via Python for Selenium with headless Chrome

how to set proxy in python 3.x httplib2?

How do you run headless chrome and a proxy using selenium in python?

how to set proxy with authentication in selenium chromedriver python?

Can not set Luminati proxy on Selenium in Python 3

How to set up a python application with selenium in a docker container

Set up proxy on selenium geckodriver

How to set proxy AUTHENTICATION username:password using Python/Selenium

How to set preferences for Chrome in Selenium Python

selenium & chrome 78 failing basic authentication

Python: Selenium webdriver Chrome opens up browser but not the site selected

How can I set up the python environment variable on Windows 7?

Python selenium use the same window instead of having 3 pop up?

How to set proxy while using urllib3.PoolManager in python

How to set up a Selenium Python environment for Firefox

Python selenium PhantomJS proxy

pod set up is failing Mac OS Sierra

Multiprocessing selenium in Python 3 using Chrome issues

Set up proxy for selenide?

Python script that uses Selenium webdriver for Chrome, trying (and failing) to print elements contained in a listbox

Python requests with proxy failing for WinError 10060