From 61fc09d56c06417cfa7b90e6c6a61150c9ad45fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ot=C3=A1vio?= Date: Sat, 28 Jan 2023 11:03:52 -0300 Subject: [PATCH] Selenium - Automatizando tarefas no navegador --- aula193/main.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/aula193/main.py b/aula193/main.py index 60f08aa..090ce7b 100644 --- a/aula193/main.py +++ b/aula193/main.py @@ -1 +1,48 @@ -print('Hello world!') +# Selenium - Automatizando tarefas no navegador +from pathlib import Path +from time import sleep + +from selenium import webdriver +from selenium.webdriver.chrome.service import Service + +# Chrome Options +# https://peter.sh/experiments/chromium-command-line-switches/ + + +# Caminho para a raiz do projeto +ROOT_FOLDER = Path(__file__).parent +# Caminho para a pasta onde o chromedriver está +CHROME_DRIVER_PATH = ROOT_FOLDER / 'drivers' / 'chromedriver' + + +def make_chrome_browser(*options: str) -> webdriver.Chrome: + chrome_options = webdriver.ChromeOptions() + + # chrome_options.add_argument('--headless') + if options is not None: + for option in options: + chrome_options.add_argument(option) # type: ignore + + chrome_service = Service( + executable_path=str(CHROME_DRIVER_PATH), + ) + + browser = webdriver.Chrome( + service=chrome_service, + options=chrome_options + ) + + return browser + + +if __name__ == '__main__': + # Example + # options = '--headless', '--disable-gpu', + options = () + browser = make_chrome_browser(*options) + + # Como antes + browser.get('https://www.google.com') + + # Dorme por 10 segundos + sleep(10)