From 2dcfbf29432839c126229e164bfd94309aa02389 Mon Sep 17 00:00:00 2001 From: Mathieu Benoit Date: Tue, 6 Feb 2024 23:27:34 -0500 Subject: [PATCH] [FIX] script selenium: web login support firefox snap --- script/selenium/install.md | 4 ++++ script/selenium/web_login.py | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/script/selenium/install.md b/script/selenium/install.md index 448a05d..db23680 100644 --- a/script/selenium/install.md +++ b/script/selenium/install.md @@ -1,3 +1,7 @@ +wget https://github.com/mozilla/geckodriver/releases/download/v0.34.0/geckodriver-v0.34.0-linux64.tar.gz +tar xvf geckodriver-v0.34.0-linux64.tar.gz +sudo mv ./.venv/geckodriver /usr/bin/geckdriver + sudo apt install libcairo2-dev python3-dev pkg-config libxt-dev libgirepository1.0-dev pip install pycairo PyGObject diff --git a/script/selenium/web_login.py b/script/selenium/web_login.py index c752bd3..287f678 100755 --- a/script/selenium/web_login.py +++ b/script/selenium/web_login.py @@ -10,6 +10,8 @@ from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait from selenium.common.exceptions import NoSuchElementException +from selenium.webdriver.firefox.service import Service +from subprocess import getoutput def get_config(): @@ -55,6 +57,14 @@ def get_config(): " like it!" ), ) + parser.add_argument( + "--firefox_binary_path", + help="Can specify firefox path to open selenium.", + ) + parser.add_argument( + "--gecko_binary_path", + help="Can specify firefox path to open selenium.", + ) parser.add_argument( "--url", default="http://127.0.0.1:8069", @@ -109,9 +119,30 @@ def run(config): firefox_options.set_preference( "permissions.default.desktop-notification", 1 ) + firefox_services = None + if config.firefox_binary_path: + firefox_services = Service(executable_path=config.firefox_binary_path) + if config.gecko_binary_path: + firefox_options.binary_location = config.gecko_binary_path # Créez une instance du navigateur Firefox avec les options de navigation privée - driver = webdriver.Firefox(options=firefox_options) + try: + driver = webdriver.Firefox( + options=firefox_options, service=firefox_services + ) + except Exception: + print("Cannot open Firefox profile, so will force firefox snap for Ubuntu users.") + firefox_services = Service( + executable_path=getoutput( + "find /snap/firefox -name geckodriver" + ).split("\n")[-1] + ) + firefox_options.binary_location = getoutput( + "find /snap/firefox -name firefox" + ).split("\n")[-1] + driver = webdriver.Firefox( + options=firefox_options, service=firefox_services + ) # Ajout de l'enregistrement if config.record_mode: