2016-01-22 34 views
0

我正在尝试创建一个类,该类将有一个一个地测试http代理的方法,直到我有一个可以添加到硒webdriver实例的工作方法。我的硒浏览器将不会在Python中关闭

我有一个raw_input来验证代理是否在webdriver中工作,如果它不工作,它应该测试另一个代理并要求我确认,然后关闭驱动程序或保持打开状态。 (我删除了使用测试方法的if语句,因为我得到一个错误)

当我在终端输入'n'时,当我被要求确认时,它关闭浏览器并用另一个代理打开另一个,但是当我第二次浏览器保持打开状态时输入'n'。

class Driver: 

    def test(self): 
     try: 
      urllib.urlopen(
       "https://www.google.com", 
       proxies={'http': proxy} 
      ) 
      return True 
     except IOError: 
      print "Connection error! (Check proxy)" 
     else: 
      return False 

    def get_driver(self): 
     proxies = [] 
     with open('working_proxies.txt', 'rb') as working_proxies: 
      for proxy in working_proxies: 
       proxy.rstrip() 
       proxies.append(proxy.decode('ascii')) 
     for i in proxies: 
      try: 
       myproxy = proxies.pop() 
       proxy = Proxy({ 
           'proxyType': ProxyType.MANUAL, 
           'httpProxy': myproxy, 
           'ftpProxy': myproxy, 
           'sslProxy': myproxy, 
           'noProxy': '' # set this value as desired 
           }) 
       driver = webdriver.Firefox(proxy=proxy) 
       is_working = raw_input('Is your proxy working? [y/n]: ') 
       if is_working == 'y' or is_working == 'Y': 
        return driver 
       if is_working == 'n' or is_working == 'N': 
        driver.close() 
        continue 
       if not is_working == 'y' or is_working == 'Y' or is_working == 'n' or is_working == 'N': 
        print 'Invallid' 
      except: 
       continue 

driver = Driver() 
driver = driver.get_driver() 
driver.get("https://www.google.com") 
+0

为什么不使用硒和断言,以确定一个工作代理,而不需要原始输入? –

回答

0

你可以试试driver.quit()代替driver.close()