【问题标题】:Python / Selenium : ERRNO 111 when I call my function multiple timesPython / Selenium:当我多次调用我的函数时出现 ERRNO 111
【发布时间】:2018-07-25 12:14:08
【问题描述】:

我目前正在学习 python,但我在多次调用一个函数时遇到了困难 使用 selenium 和 geckodriver。

这段代码在我使用一次时运行良好:

from selenium import webdriver
from selenium.webdriver.support.ui  import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.firefox.options import Options
import time
import sms

options = Options()
options.add_argument("--headless")
profile = webdriver.FirefoxProfile()
profile.set_preference("dom.disable_beforeunload", True)
browser = webdriver.Firefox(firefox_options=options, firefox_profile=profile)
timeout=10




def busETA(trajet):




    if trajet == "aller" :
        print('Recherche en cours du trajet aller...')
        Arret="Maison de retraite"
        #URL BONNEVILLE => CHAL

        URL='https://pysae.com/info/#/sm4cc/map/stop/5'
        browser.get(URL)
        #URL CHAL => BONNEVILLE
        #Arrêt Maison de retraite
        try :
            EtaPresent = EC.presence_of_element_located((By.CSS_SELECTOR, '.firstTime > span:nth-child(1)'))
            WebDriverWait(browser,timeout).until(EtaPresent)
            ETA = browser.find_element_by_css_selector('.firstTime > span:nth-child(1)').text
            if "H" not in ETA:
                ETA=ETA+' min'
            reponse = 'Le prochain bus arrivera dans '+str(ETA)+" à l'arrêt: "+Arret


        except WebDriverException as e:
            try :
                ETA = browser.find_element_by_css_selector('.text-endService').text
                reponse=ETA
            except WebDriverException as e:
                print("Error aller")
                ETA="Erreur"
                pass
        finally : 
            browser.quit()



    elif trajet =="retour" : 
        print('Recherche en cours du trajet retour...')
        Arret="CHAL"
        URL='https://pysae.com/info/#/sm4cc/map/stop/10'
        browser.get(URL)
        #Arrêt CHAL
        try :
            EtaPresent = EC.presence_of_element_located((By.CSS_SELECTOR, '.routes-view > footer:nth-child(2) > div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > span:nth-child(3) > span:nth-child(1) > span:nth-child(1)'))
            WebDriverWait(browser,timeout).until(EtaPresent)
            ETA= browser.find_element_by_css_selector('.routes-view > footer:nth-child(2) > div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > span:nth-child(3) > span:nth-child(1) > span:nth-child(1)').text
            if "H" not in ETA:
                ETA=ETA+' min'
            reponse = 'Le prochain bus arrivera dans '+str(ETA)+" à l'arrêt: "+Arret

        except WebDriverException as e:
            try :
                ETA = browser.find_element_by_css_selector('.text-endService').text
                reponse=ETA
            except WebDriverException as e:
                print("Erreur retour")
                ETA="Erreur"
                browser.quit()
                pass
        finally :  
            browser.quit()


    print(reponse)
    #sms.sendsms(reponse,'number')
    return

busETA('retour')

但如果我这样称呼它更多次:

busETA('retour')
busETA('aller')
busETA('retour')

我得到这个错误:

    Recherche en cours du trajet retour...
Le prochain bus arrivera dans 26 min à l'arrêt: CHAL                                                                                                                                      
Recherche en cours du trajet retour...                                                                                                                                                    
Traceback (most recent call last):                                                                                                                                                        
  File "Pysae.py", line 90, in <module>                                                                                                                                                   
    busETA('retour')                                                                                                                                                                      
  File "Pysae.py", line 62, in busETA                                                                                                                                                     
    browser.get(URL)                                                                                                                                                                      
  File "/home/sirefen74/dev/python/ProxiPy/venv/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 332, in get                                                     
    self.execute(Command.GET, {'url': url})                                                                                                                                               
  File "/home/sirefen74/dev/python/ProxiPy/venv/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 318, in execute                                                 
    response = self.command_executor.execute(driver_command, params)                                                                                                                      
  File "/home/sirefen74/dev/python/ProxiPy/venv/lib/python3.5/site-packages/selenium/webdriver/remote/remote_connection.py", line 472, in execute                                         
    return self._request(command_info[0], url, body=data)                                                                                                                                 
  File "/home/sirefen74/dev/python/ProxiPy/venv/lib/python3.5/site-packages/selenium/webdriver/remote/remote_connection.py", line 495, in _request                                        
    self._conn.request(method, parsed_url.path, body, headers)                                                                                                                            
  File "/usr/lib/python3.5/http/client.py", line 1106, in request                                                                                                                         
    self._send_request(method, url, body, headers)                                                                                                                                        
  File "/usr/lib/python3.5/http/client.py", line 1151, in _send_request                                                                                                                   
    self.endheaders(body)                                                                                                                                                                 
  File "/usr/lib/python3.5/http/client.py", line 1102, in endheaders                                                                                                                      
    self._send_output(message_body)                                                                                                                                                       
  File "/usr/lib/python3.5/http/client.py", line 934, in _send_output                                                                                                                     
    self.send(msg)                                                                                                                                                                        
  File "/usr/lib/python3.5/http/client.py", line 877, in send                                                                                                                             
    self.connect()                                                                                                                                                                        
  File "/usr/lib/python3.5/http/client.py", line 849, in connect                                                                                                                          
    (self.host,self.port), self.timeout, self.source_address)                                                                                                                             
  File "/usr/lib/python3.5/socket.py", line 711, in create_connection                                                                                                                     
    raise err                                                                                                                                                                             
  File "/usr/lib/python3.5/socket.py", line 702, in create_connection                                                                                                                     
    sock.connect(sa)                                                                                                                                                                      
ConnectionRefusedError: [Errno 111] Connection refused

我必须承认我对 python 很陌生...

我做错了什么?

任何帮助将不胜感激!

【问题讨论】:

    标签: python selenium geckodriver


    【解决方案1】:

    我已经通过在函数中调用 webdriver 解决了这个问题:

    def busETA(trajet):
    browser = webdriver.Firefox(firefox_options=options)
    [...]
    

    所以每次新调用它都会重新打开浏览器!

    实际上我是在用 browser.quit() 破坏浏览器 没有再次打开它,导致 ERRNO 111 错误。

    【讨论】:

      猜你喜欢
      • 2019-08-10
      • 1970-01-01
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 2016-05-30
      • 1970-01-01
      • 2019-02-16
      • 2020-04-25
      相关资源
      最近更新 更多