Headless Chrome (with selenium) CANNOT request with PROXY server, but requests can?
Asked Answered
M

1

7

I am trying to use Chrome along with python webdriver + selenium, but it seems not working when I set the proxy settings? Here is my code:

from selenium import webdriver

PROXY = 'http://42.115.88.220:53281'
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--proxy-server=%s' % PROXY)
chromeOptions.add_argument("ignore-certificate-errors")

wbe = webdriver.Chrome(options=chromeOptions)
wbe.get("http://icanhazip.com")

When I run the above codes, the browser gives me: "This site can’t be reached" error:

This site can’t be reached

The connection was reset.
Try:
  • Checking the connection
  • Checking the proxy and the firewall
  • Running Windows Network Diagnostics

ERR_CONNECTION_RESET

Some Efforts: I tried requests with my proxy server, and it works. So it shouldn't be the problem of my proxy server.

import requests

proxies = {"http": "http://42.115.88.220:53281"}
r = requests.get("http://icanhazip.com", proxies = proxies)
print (r.status_code)

This gives me a response code of 200 and good response.

Goal: My final goal is to build a web-crawler with headless chrome with PROXY, so now I am testing a non-headless one first. But it seems there have been something wrong with this PROXY issue.

I would be really appreciated if anyone could help me out with this problem!!!

Mores answered 25/1, 2019 at 4:16 Comment(6)
Does changing ignore-certificate-errors to --ignore-certificate-errors have any effect?Nusku
Please follow the below link that i have sharedPiste
#27730806Piste
github.com/oesmith/puffing-billy/issues/193Piste
@Nusku no, there's not. It is not working even when I removes '--ignore-certificate-errors' parameterMores
@akshaypatil I tried what you've shared, and they are not working still...But anyway thanks.Mores
I
0

Try this. For me it seems that you have used wrong type of headless mode. For chrome selenium browsers it's important to set --headless argument correct.

from selenium import webdriver

PROXY = 'http://ip:port'
chromeOptions = webdriver.ChromeOptions() 
chromeOptions.add_argument('--proxy-server=%s' % PROXY) 
chromeOptions.add_argument("ignore-certificate-errors")
# Headless mode for chrome browser
chromeOptions.add_argument('--headless=chrome')
wbe = webdriver.Chrome('your_driver_path_or_service', options=chromeOptions) 
wbe.get("http://icanhazip.com")
print(wbe.title)
print(wbe.current_url)
print(wbe.page_source)

# Output:
# http://icanhazip.com/
# <html><head><meta name="color-scheme" content="light dark"></head><body><pre 
# style="word-wrap: break-word; white-space: pre-wrap;">your ip
# </pre></body></html>
Invent answered 13/12, 2022 at 12:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.