8

How can I use proxy server using selenium and google chrome? I attached the code and I'm not sure if this will change the actual proxy server.

# selenium imports
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import random

PROXY ="88.157.149.250:8080";


chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)
# //a[starts-with(@href, 'https://www.amazon.com/')]/@href
LINKS_XPATH = '//*[contains(@id,"result")]/div/div[3]/div[1]/a'
browser = webdriver.Chrome(executable_path="C:\\Users\Andrei\Downloads\chromedriver_win32\chromedriver.exe",
                           chrome_options=chrome_options)
browser.get(
    'https://www.amazon.com/s/ref=lp_11444071011_nr_p_8_1/132-3636705-4291947?rh=n%3A3375251%2Cn%3A%213375301%2Cn%3A10971181011%2Cn%3A11444071011%2Cp_8%3A2229059011')
links = browser.find_elements_by_xpath(LINKS_XPATH)
for link in links:
    href = link.get_attribute('href')
    print(href)
DavidG
  • 24,279
  • 14
  • 89
  • 82
ryy77
  • 1,134
  • 5
  • 20
  • 36

3 Answers3

16
from selenium import webdriver

PROXY = "88.157.149.250:8080" # IP:PORT or HOST:PORT

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(f"--proxy-server={PROXY}")

chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get("http://google.com")
mirekphd
  • 4,799
  • 3
  • 38
  • 59
Hiten
  • 724
  • 1
  • 8
  • 23
  • 1
    Thank you! How can I test if the ip is replaced? – ryy77 Jan 29 '18 at 14:01
  • 1
    Is there an example using Socks5 authentication with username and password? I receive the error ERR_NO_SUPPORTED_PROXIES when attempting to set the PROXY in the following format: user:pass@IP/HOST:PORT. – Liquidgenius Jun 27 '18 at 17:01
  • 2
    using this code with any of the free IPs here http://spys.one/en/https-ssl-proxy/ then redirecting to a 'what is my IP page', my IP never changes. – user2723494 Oct 30 '18 at 14:21
  • If the proxy server requires authentication, it requires a bit more work to connect to it, such as creating a Chrome extension. See https://stackoverflow.com/a/35293284 for details. – Michael Mintz Apr 10 '19 at 06:08
  • you should change chrome_options to options in Chrome method – mamal Jun 09 '20 at 12:14
4

You can open the page https://www.whatismyip.com/my-ip-information/

chrome.get("https://www.whatismyip.com/my-ip-information/")
ah bon
  • 9,293
  • 12
  • 65
  • 148
0
proxy = 192.168.22.1:8080
if proxy != None:
    print('\nProxy ativado: ',proxy)
    #chrome_options.add_argument('--proxy-server=%s' % proxy)
    webdriver.DesiredCapabilities.CHROME['proxy'] = {
        "httpProxy": proxy,
        "ftpProxy": proxy,
        "sslProxy": proxy,
        "proxyType": "MANUAL",

    }

webdriver.DesiredCapabilities.CHROME['acceptSslCerts']=True
print(webdriver.DesiredCapabilities.CHROME)