2016-01-22 33 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()