Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access country restricted website through proxy selenium in python

I am trying to access a website through selenium which is blocked in the country I live in. I am using selenium in python and using a proxy to do this. However, I noticed that once I check the IP of the opened selenium browser, it shows my real IP and not the proxy. It is a bit confusing because I managed to access this site once through the following driver settings, but it doesn't work anymore.

        fp = webdriver.FirefoxProfile()
        PROXY_PORT = config['DEFAULT']['PROXY_PORT']
        PROXY_HOST = config['DEFAULT']['PROXY_HOST']
        fp.set_preference('network.proxy.type', 0)
        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", "whater_useragent")
        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)

Now, I am trying using assigning the proxy using this method but it makes no change.

    PROXY_PORT = config['DEFAULT']['PROXY_PORT']
    PROXY_HOST = config['DEFAULT']['PROXY_HOST']
    myProxy = PROXY_HOST + ':' + PROXY_PORT
    proxy = webdriver.common.proxy.Proxy({'proxyType':webdriver.common.proxy.ProxyType.MANUAL,
     'httpProxy':myProxy,
     'ftpProxy':myProxy,
     'sslProxy':myProxy})
    driver = webdriver.Firefox(proxy=proxy)

What I need is for the IP in the opened firefox browser to be that of the proxy. How can this be done? I have no idea why it worked at first an now it doesn't. Please explain.

like image 634
toing_toing Avatar asked May 13 '18 21:05

toing_toing


People also ask

Can you use proxies with selenium?

We can run a proxy with Selenium webdriver in Python. A proxy is an essential component to do localization testing. We can take an e-commerce application and check if the language and currency visible is as per the user location. Import webdriver from Selenium package.

What is proxy element in selenium?

A proxy acts as an intermediary between clients sending requests and server responding. The primary use of a proxy is to maintain privacy and encapsulation between multiple interactive systems.


1 Answers

Using Chrome, it's very simple:

options = webdriver.ChromeOptions()
options.add_argument('='.join(['--proxy-server', "http://localhost:8888/"]))

You can also try using an addon/extension to do this. I've had success both ways :)

In case you need a proxy server: https://github.com/ochen1/pyproxy

like image 53
idontknow Avatar answered Oct 17 '22 04:10

idontknow