0

I am using selenium with python. I need to configure a proxy.

It is working for HTTP but not for HTTPS.

The code I am using is:

# configure firefox
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", '11.111.11.11')
profile.set_preference("network.proxy.http_port", int('80'))
profile.update_preferences()

# launch
driver = webdriver.Firefox(firefox_profile=profile)
driver.get('https://www.iplocation.net/find-ip-address')

Also. Is there a way for me to completely block any outgoing traffic from my IP and restrict it ONLY to the proxy IP so that I don't accidently mess up the test/stats by accidently switching from proxy to direct connection?

Any tips would help! Thanks :)

willer2k
  • 506
  • 1
  • 6
  • 17

2 Answers2

1

Check out browsermob proxy for setting up a proxies for use with selenium

from browsermobproxy import Server
server = Server("path/to/browsermob-proxy")
server.start()
proxy = server.create_proxy()

from selenium import webdriver
profile  = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)


proxy.new_har("google")
driver.get("http://www.google.co.uk")
proxy.har # returns a HAR JSON blob

server.stop()
driver.quit()

You can use a remote proxy server with the RemoteServer class.

Is there a way for me to completely block any outgoing traffic from my IP and restrict it ONLY to the proxy IP

Yes, just look up how to setup proxies for whatever operating system you're using. Just use caution because some operating systems will ignore proxy rules based on certain conditions, for example, if using a VPN connection.

sytech
  • 29,298
  • 3
  • 45
  • 86
  • Why is this required? Is selenium not able to provide proxy on its own? Why do we need browsermobproxy? I appreciate the information so far, will do some reading. Thanks :) – willer2k Oct 19 '16 at 02:16
  • It is useful because you have access to the HAR data from the proxy server, which would offer an assurance the proxy is being used, among other benefits. To get this data otherwise would be a somewhat involved process. Browsermob proxy abstracts this with a uniform API. – sytech Oct 19 '16 at 02:50
0

Here is the answer of it. You can try this one.

for PROXY you can use any proxy host with port.

https://stackoverflow.com/a/48498403/5533485

Hiten
  • 724
  • 1
  • 8
  • 23