python-2.7seleniumproxyubuntu-serverfirefox-headless

python proxy in selenium firefox headless not working


I am using Ubuntu Server 17.04.

I am trying to set my proxy into selenium. But it's not working. I am using proxy from https://stormproxies.com/. Basically only the ip i allowed it will be allowed to have proxy and then i have to use the ip that they give me to access it. So basically it's not anything like this "ip:port@user:pass". Let's say this is the proxy ip i m using

Example: https://123.123.123.123:13028

The proxy works fine...cause I used it in scrapy.

But not working in in selenium..I have tried all the examples that I found and it gives me all kinds of errors. I'm thinking is because the examples are out-dated...maybe.

My selenium and everything it's up to date. I have geckodriver set to path...and everything. Selenium works fine if I don't add proxy.

Those are the main imports I am using for all.

from selenium import webdriver
from selenium.webdriver.common.proxy import *
from pyvirtualdisplay import Display

Those are all the scripts I have tried.

1st Example it doesn't give me any error but it doesn't connect to the proxy. when I try to get the ip in "https://whatismyipaddress.com" I get my true ip, not a proxy

display = Display(visible=0, size=(1920, 1080)).start()

myProxy = "https://123.123.123.123:13028"

proxy = Proxy({
    'proxyType': ProxyType.MANUAL,
    'httpProxy': myProxy,
    'ftpProxy': myProxy,
    'sslProxy': myProxy,
    'noProxy': None
})
browser = webdriver.Firefox()
browser.get("https://whatismyipaddress.com/")

2nd Example - it gives me this error: WebDriverException: Message: Process unexpectedly closed with status: 1

proxies = "123.123.123.123:13028"
prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = proxies
prox.socks_proxy = proxies
prox.ssl_proxy = proxies

capabilities = webdriver.DesiredCapabilities.FIREFOX
prox.add_to_capabilities(capabilities)

driver = webdriver.Firefox(capabilities=capabilities)

3rd Example - it gives me this error: WebDriverException: Message: Process unexpectedly closed with status: 1

PROXY_PORT = '13028'
PROXY_HOST = '123.123.123.123'

fp = webdriver.FirefoxProfile()
print PROXY_PORT
print PROXY_HOST
fp.set_preference("network.proxy.type", 1)
fp.set_preference("network.proxy.http", PROXY_HOST)
fp.set_preference("network.proxy.http_port", int(PROXY_PORT))
fp.set_preference("network.proxy.https", PROXY_HOST)
fp.set_preference("network.proxy.https_port", int(PROXY_PORT))
fp.set_preference("network.proxy.ssl", PROXY_HOST)
fp.set_preference("network.proxy.ssl_port", int(PROXY_PORT))
fp.set_preference("network.proxy.ftp", PROXY_HOST)
fp.set_preference("network.proxy.ftp_port", int(PROXY_PORT))
fp.set_preference("network.proxy.socks", PROXY_HOST)
fp.set_preference("network.proxy.socks_port", int(PROXY_PORT))
fp.set_preference("general.useragent.override",
                  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A")
fp.update_preferences()

driver = webdriver.Firefox(firefox_profile=fp)

4th Example - it gives me this error : InvalidArgumentException: Message: null is not an array

PROXY = "123.123.123.123:13028"
webdriver.DesiredCapabilities.FIREFOX['proxy'] = {
    "httpProxy": PROXY,
    "ftpProxy": PROXY,
    "sslProxy": PROXY,
    "noProxy": None,
    "proxyType": "MANUAL",
}
driver = webdriver.Firefox()
driver.get('http://www.whatsmyip.org/')

And i also used this example:

5th Example - it gives me this error: WebDriverException: Message: Process unexpectedly closed with status: 1

ProxyHost = "123.123.123.123"
ProxyPort = "13028"


def ChangeProxy(ProxyHost, ProxyPort):
    "Define Firefox Profile with you ProxyHost and ProxyPort"
    profile = webdriver.FirefoxProfile()
    profile.set_preference("network.proxy.type", 1)
    profile.set_preference("network.proxy.http", ProxyHost)
    profile.set_preference("network.proxy.http_port", int(ProxyPort))
    profile.update_preferences()
    return webdriver.Firefox(firefox_profile=profile)


def FixProxy():
    # ""Reset Firefox Profile""
    profile = webdriver.FirefoxProfile()
    profile.set_preference("network.proxy.type", 0)
    return webdriver.Firefox(firefox_profile=profile)


driver = ChangeProxy(ProxyHost, ProxyPort)
driver.get("http://whatismyipaddress.com")

time.sleep(5)

driver = FixProxy()
driver.get("http://whatismyipaddress.com")

Solution

  • You just need to add the firefox_options element to the driver when you initialise it.

    Make sure you are importing the proxy side properly(obviously)

    from selenium.webdriver.common.proxy import Proxy, ProxyType
    

    then initialise your driver like this

    options = Options()
    options.add_argument("--headless")
    
    ProxyHost = "x.x.x.x" 
    ProxyPort = "8118"
    
    def SetProxy(ProxyHost ,ProxyPort):
    
        profile = webdriver.FirefoxProfile()
        profile.set_preference("network.proxy.type", 1)
        profile.set_preference("network.proxy.http", ProxyHost )
        profile.set_preference("network.proxy.http_port", int(ProxyPort))
        profile.update_preferences()
        return webdriver.Firefox(firefox_profile=profile, firefox_options=options)
    
    
    try:
            driver = SetProxy(ProxyHost ,ProxyPort)
            driver.get("http://x.x.x.x")
            print(driver.page_source)
            driver.close()
    except:
            traceback.print_exc()
            driver.close()