Reputation: 51
I've been trying for about 2 days to use selenium with a proxy server but have still not been able to do it. I have tried the method from the selenium website, copy and pasting but it didn't work. I've tried all of the answers from Stackoverflow but none of them worked, the closest I got was loading the page but my IP stayed the same.
Here is the code I tried most recently:
#!/usr/bin/env python
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
HOST = "144.217.31.225"
PORT = "3128"
def my_proxy(PROXY_HOST,PROXY_PORT):
fp = webdriver.FirefoxProfile()
# Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5
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("general.useragent.override","whater_useragent")
fp.update_preferences()
return webdriver.Firefox(firefox_profile=fp)
browser = my_proxy(HOST,PORT)
browser.get('https://www.google.com')
search=browser.find_element_by_xpath("//input[@title='Search']")
search.send_keys('my ip')
This is the error I received:
3128
144.217.31.225
Traceback (most recent call last):
File "./script.py", line 32, in <module>
search=browser.find_element_by_xpath("//input[@title='Search']")
File "/home/matt/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 313, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/home/matt/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 791, in find_element
'value': value})['value']
File "/home/matt/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
self.error_handler.check_response(response)
File "/home/matt/.local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //input[@title='Search']
It manages to load the page but fails to enter the text in the search bar. I manually take over and type 'my IP' in the search bar but the IP hasn't changed.
Upvotes: 1
Views: 5003
Reputation: 51
This following code worked, or though google didn't like the fact I was using a proxy with selenium, this is why I changed the address to whatsmyipaddress.com
.
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from time import sleep
HOST = "144.217.31.225"
PORT = "3128"
def my_proxy(PROXY_HOST,PROXY_PORT):
fp = webdriver.FirefoxProfile()
# Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5
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.ssl",PROXY_HOST)
fp.set_preference("network.proxy.ssl_port",int(PROXY_PORT))
fp.set_preference("general.useragent.override","whater_useragent")
fp.update_preferences()
return webdriver.Firefox(firefox_profile=fp)
browser = my_proxy(HOST,PORT)
browser.get('http://whatismyipaddress.com/')
sleep(5)
search=browser.find_element_by_xpath("//input[@title='Search']")
search.send_keys('my ip')
Thanks to @SAZ for the solution.
Upvotes: 2