Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Selenium Webdriver with a proxy in Python

I am trying to run a Selenium Webdriver script in Python to do some basic tasks. I can get the robot to function perfectly when running it through the Selenium IDE inteface (ie: when simply getting the GUI to repeat my actions). However when I export the code as a Python script and try to execute it from the command line, the Firefox browser will open but cannot ever access the starting URL (an error is returned to command line and the program stops). This is happening me regardless of what website etc I am trying to access.

I have included a very basic code here for demonstration purposes. I don't think that I have included the proxy section of the code correctly as the error being returned seems to be generated by the proxy.

Any help would be hugely appreciated.

The below code is simply meant to open www.google.ie and search for the word "selenium". For me it opens a blank firefox browser and stops.

from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select from selenium.common.exceptions import NoSuchElementException import unittest, time, re from selenium.webdriver.common.proxy import *  class Testrobot2(unittest.TestCase):     def setUp(self):          myProxy = "http://149.215.113.110:70"          proxy = Proxy({         'proxyType': ProxyType.MANUAL,         'httpProxy': myProxy,         'ftpProxy': myProxy,         'sslProxy': myProxy,         'noProxy':''})          self.driver = webdriver.Firefox(proxy=proxy)         self.driver.implicitly_wait(30)         self.base_url = "https://www.google.ie/"         self.verificationErrors = []         self.accept_next_alert = True      def test_robot2(self):         driver = self.driver         driver.get(self.base_url + "/#gs_rn=17&gs_ri=psy-ab&suggest=p&cp=6&gs_id=ix&xhr=t&q=selenium&es_nrs=true&pf=p&output=search&sclient=psy-ab&oq=seleni&gs_l=&pbx=1&bav=on.2,or.r_qf.&bvm=bv.47883778,d.ZGU&fp=7c0d9024de9ac6ab&biw=592&bih=665")         driver.find_element_by_id("gbqfq").clear()         driver.find_element_by_id("gbqfq").send_keys("selenium")      def is_element_present(self, how, what):         try: self.driver.find_element(by=how, value=what)         except NoSuchElementException, e: return False         return True      def is_alert_present(self):         try: self.driver.switch_to_alert()         except NoAlertPresentException, e: return False         return True      def close_alert_and_get_its_text(self):         try:             alert = self.driver.switch_to_alert()             alert_text = alert.text             if self.accept_next_alert:                 alert.accept()             else:                 alert.dismiss()             return alert_text         finally: self.accept_next_alert = True      def tearDown(self):         self.driver.quit()         self.assertEqual([], self.verificationErrors)  if __name__ == "__main__":     unittest.main() 
like image 563
user2479813 Avatar asked Jun 13 '13 08:06

user2479813


People also ask

How do I enable proxy in Python?

To use a proxy in Python, first import the requests package. Next create a proxies dictionary that defines the HTTP and HTTPS connections. This variable should be a dictionary that maps a protocol to the proxy URL. Additionally, make a url variable set to the webpage you're scraping from.

Can you use Python with selenium WebDriver?

Selenium is an open source automation testing tool that supports a number of scripting languages like Python, C#, Java, Perl, Ruby, JavaScript, etc.


2 Answers

Works for me this way (similar to @Amey and @user4642224 code, but shorter a bit):

from selenium import webdriver from selenium.webdriver.common.proxy import Proxy, ProxyType  prox = Proxy() prox.proxy_type = ProxyType.MANUAL prox.http_proxy = "ip_addr:port" prox.socks_proxy = "ip_addr:port" prox.ssl_proxy = "ip_addr:port"  capabilities = webdriver.DesiredCapabilities.CHROME prox.add_to_capabilities(capabilities)  driver = webdriver.Chrome(desired_capabilities=capabilities) 
like image 181
Mykhail Martsyniuk Avatar answered Oct 06 '22 06:10

Mykhail Martsyniuk


How about something like this

PROXY = "149.215.113.110:70"  webdriver.DesiredCapabilities.FIREFOX['proxy'] = {     "httpProxy":PROXY,     "ftpProxy":PROXY,     "sslProxy":PROXY,     "noProxy":None,     "proxyType":"MANUAL",     "class":"org.openqa.selenium.Proxy",     "autodetect":False }  # you have to use remote, otherwise you'll have to code it yourself in python to  driver = webdriver.Remote("http://localhost:4444/wd/hub", webdriver.DesiredCapabilities.FIREFOX) 

You can read more about it here.

like image 26
Amey Avatar answered Oct 06 '22 07:10

Amey