I'm currently learning python and I struggle with multiple calls of a function using selenium with geckodriver.
This code is working well when I use it a single time :
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')
But If I call it more times like this:
busETA('retour')
busETA('aller')
busETA('retour')
I get this error :
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
I must admit that I'm pretty new to python...
What did I get wrong ?
Any help will be greatly appreciated !
I've fixed the problem by calling the webdriver inside the function :
def busETA(trajet):
browser = webdriver.Firefox(firefox_options=options)
[...]
so every new call it re-open the browser !
In fact I was destroying the browser with browser.quit() without opening it again, causing the ERRNO 111 error.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.